how do I multiply the value in python

1

How do I want the result to be 4 and not to be 'aa' of the following source code?

set1=input ('Entra letra para multiplicar su valor en numero x2: ')
a=2
b=3
c=4
d=5
e=6
muestra=2*set1
print (muestra)
    
asked by Juan Carlos Villamizar Alvarez 19.07.2018 в 06:30
source

2 answers

2

What you read with input is a string. In your case, if the user answers a , the value of set1 is the string "a" , so logically when multiplying by two the chain multiplication operator is used whose mission is to repeat the string the number of indicated times and thus comes out "aa" .

What you need is to convert the string "a" to the value 2 (and the string "b" to 3, etc.)

Here are several possibilities:

1. Are those values in concrete? (solution with ord )

That is, do you always want "a" to be 2, "b" to be 3, etc. or was that just an example?

If those particular values are what you want to use, since they grow from 2 to 6 in alphabetical order of the name of the variable, you can use the ascii code of the user's letter as a starting point to convert it into a number:

muestra = (ord(set1)-ord("a")+2)*2

ord() gives you the code of the letter. By subtracting the code from "a" , if the letter was "a" 0, if it was "b" 1, etc. It is enough to add 2.

2. If the values can be others (solution with dict )

In that case you can have a dictionary as a translation table from letter to number, as FJSevilla's comment suggests:

traduccion = dict(a=1, b=2, c=3, d=4, e=5)
if set1 not in traduccion:
    print("La letra introducida no es válida")
else:
    muestra = 2 * traduccion[set1]
    print(muestra)

3. If they have to be variable (solution with eval )

That is, if the dictionary solution is not valid. In this case you could try to construct an expression in the form of a string:

expresion = "2*{}".format(set1)

If the user typed "a" , then expresion would be the string "2*a" . Then you can use eval(expresion) for Python to "execute" that string as if it were a python expression within a program. This would treat the a that appears in the expression as a variable, and correctly calculate the result sought.

However you should not do this . eval() is dangerous and using it directly to evaluate strings that have been entered by the user puts you at risk. Imagine that the user, instead of typing "a" type "[1]*1000000" , in that case the expresion would be "2*[1]*1000000" and when python evaluates it will create a list composed of two million ones, which will take a while and it will consume a lot of memory. Of course, the user can enter even larger numbers and cause your program to stop responding, which, if your program were part of a server, would be a denial of service attack.

4. If they have to be variable (solution without eval )

Python has the function globals() that returns a dictionary whose keys are all global variables, and whose values are those that have those variables. Analogously it also has locals() for the local variables of the function that is running.

So this solution is equivalent to 2, only you do not have to create the dictionary but it is already created with the variables of your program. Using this approach:

traduccion = globals()
if set1 not in traduccion:
   print("La letra introducida no es válida")
else:
   muestra = 2*traduccion[set1]
   print(muestra)
    
answered by 19.07.2018 / 10:20
source
0

As FJSevilla says the solution of the dictionaries is appropriate to your program

set1=input('Entra letra para multiplicar su valor en numero x2: ')
valor = dict()
valor['a']=2
valor['b']=3
valor['c']=4
valor['d']=5
valor['e']=6
muestra=2*valor.get(set1)
print (muestra)

It is important that you really understand what is happening, however keep in mind that solution 1 of abulafia is very pythonica but you must understand what is happening.

    
answered by 19.01.2019 в 16:14