nasher99
nasher99

Reputation: 1

i keep getting a Value error in python

I am new to programming I decided I would make a basic maths game. I started print ("what is your name") name = input () print ("hello " + name + " i am steve and this is my maths test. Are you ready? Y/N") answer_1 = input () answer_1 = int (answer_1)

if answer_1 ==Y:
    print ("Good then lets get started")

else:
    print ("well you have started now so to late lets go")

I went to test it and every time I do I get this.

what is your name
Callum
hello Callum i am steve and this is my maths test. Are you ready? Y/N
Y
Traceback (most recent call last):
  File "C:\Users\callum\Documents\programming\maths test.py", line 6, in <module>
answer_1 = int (answer_1)
ValueError: invalid literal for int() with base 10: 'Y'
>>> 

Can anyone tell me what I have done wrong

Upvotes: -1

Views: 123

Answers (4)

Odai Al-Ghamdi
Odai Al-Ghamdi

Reputation: 302

remove this line : answer_1 = int (answer_1)
and change this if answer_1 == Y: to if answer_1 == 'Y':

Upvotes: 0

mnagel
mnagel

Reputation: 6854

you are converting the string input from this line

answer_1 = input ()

into an integer here

answer_1 = int (answer_1)

this will not work if the input is "Y".

Then you compare this input with Y, but you should compare with the string "Y" here:

if answer_1 ==Y:

finally, i think it is not a good idea to have spaces in programming-related filesnames.

Upvotes: 0

Karoly Horvath
Karoly Horvath

Reputation: 96258

Simply:

if answer_1 == 'Y':

And don't convert it to an integer...

Upvotes: 2

Shashank
Shashank

Reputation: 13869

You are trying to convert the letter 'y' into a base 10 number.

Take this line out of your code.

answer_1 = int (answer_1)

Also when you are testing for string equality, remember to use quotation marks or the python interpreter won't know if you mean a variable name or the actual string 'Y'.

if answer_1 =='Y':
    print ("Good then lets get started")

Upvotes: 2

Related Questions