Reputation: 109
PhoneValue=0
if (condition== "new"):
PhoneValue=int(PhoneValue+10)
else:
PhoneValue=int(PhoneValue+9)
if GPS==bool(input("true")):
PhoneValue=int(PhoneValue+1)
else:
PhoneValue=int(PhoneValue)
if WiFi==eval(bool(input("true"))):
PhoneValue=int(PhoneValue+1)
else:
PhoneValue=int(PhoneValue)
if camera==eval(bool(input("true"))):
PhoneValue=int(PhoneValue+1)
else:
PhoneValue=int(PhoneValue)
global PhoneValue
This is my code. I am supposed to be able to input the condition, GPS, camera, and WiFi and the code evaluates the input and gives points for each condition. If the phone is new it gets ten points and if used it gets nine. For GPS, Camera, and WiFi it wants me to use boolean to either give it a point for true or no points for false. I am wondering how do I convert the input string into boolean in order to add it to phone value?
Upvotes: 0
Views: 1965
Reputation: 43487
There's a lot wrong in this code. Firstly, the input()
command is defined
input([prompt])
If the prompt argument is present, it is written to standard output without a trailing newline.
which means your call to input("true")
prints "true" on the console and waits for a line of input. That's not what you were hoping for.
Your use of eval
is bad. Almost every use of eval
on user input is a problem. But you saved yourself here by accident: eval(bool(text))
is superfluous. The only thing that bool()
can return True
or False
neither of which is dangerous to eval, but since you already had a boolean in hand, eval'ing it didn't do anything.
Converting the result of integer addition to an int()
is useless, and your if / else clauses can be more clearly written as:
if input("Is there a GPS? "):
PhoneValue += 1
with no else
clause needed. Unfortunately, this has almost no chance of getting correct input. If I type "True" the if
block will trigger. It will also trigger if I write "no", "false", or "JosEduSol", those will be evaluated as True
also. The declaration at the end
global PhoneValue
does absolutely nothing as the last line. In fact, you should probably just forget that global
exists because most everybody uses it incorrectly.
There are more faults in the code, and you should really get assistance from a teacher or get a better learning resource.
Upvotes: 1