Python Forum
How to write a response if a user inputs a string
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to write a response if a user inputs a string
#1
def main():
    """
    Create a program that randomly generates simple addition problems for the user,
    reads in the answer from the user, and then checks to see if they got it right or wrong,
    until the user appears to have mastered the material and print a message to congratulate the user.
    """

    answers_right = 0

    while answers_right < 3:

        ran_number_1 = random.randint(10, 99)
        ran_number_2 = random.randint(10, 99)
        solution = ran_number_1 + ran_number_2

        print(f"What is {ran_number_1} + {ran_number_2}?")
        user_answer = int(input("Your answer: "))

        if user_answer == solution:
            answers_right += 1
            print(f"Correct. You've gotten {answers_right} correct in a row.")
        elif user_answer != solution:
            answers_right = 0
            print(f"Incorrect. The expected answer is {solution}.")
        else:
            print("Invalid Response.")
Error:
Traceback (most recent call last): File "C:/Users/Carlo/Documents/Standford Class/Assignment2/khansole_academy.py", line 47, in <module> main() File "C:/Users/Carlo/Documents/Standford Class/Assignment2/khansole_academy.py", line 26, in main user_answer = int(input("Your answer: ")) ValueError: invalid literal for int() with base 10: 'qer'
Reply
#2
try/except
Catch that ValueError exception instead of letting it fall through to the default exception handler.
Reply
#3
I was trying to do a try/except but I'm not sure where to place it.

Any advice?
Reply
#4
There are multiple ways to solve this problem. Perhaps the simplest is convert "solution" to a string instead of converting user_answer to an int.

If you need the answer to be an integer you can catch the exception
try:
   correct_answer = solution == int(user_answer)
except ValueError:
   correct_anser = False

if correct_anser:
   # do correct answer stuff
else:
   # do incorrect answer stuff
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Multi set string inputs/outputs kwmcgreal 2 415 Sep-26-2020, 10:44 PM
Last Post: kwmcgreal
  Problem with accepting multiple string inputs Ryan_Todd 5 812 Jan-22-2020, 06:12 PM
Last Post: buran
  How to check if user entered string or integer or float?? prateek3 5 6,980 Dec-21-2019, 06:24 PM
Last Post: DreamingInsanity
  Perminantly saving user inputs + being able to retrieve it ThatOneGuyNoOneKnowsCodes 1 771 Oct-23-2019, 11:28 PM
Last Post: DT2000
  Trying to prompt user for 2 inputs on one line pythonprogrammer 2 760 Sep-15-2019, 04:41 PM
Last Post: snippsat
  Python start from a specific string line and write? searching1 1 823 Jun-27-2019, 02:28 PM
Last Post: perfringo
  write image into string format into text file venkat18 2 1,529 Jun-01-2019, 06:46 AM
Last Post: venkat18
  Using Pypdf2 write a string to a pdf file Pedroski55 6 9,119 Apr-11-2019, 11:10 PM
Last Post: snippsat
  user inputs in constructing a dictionary Exsul 3 1,325 Apr-10-2019, 12:25 PM
Last Post: ichabod801
  unit testing a method that asks two user inputs() in console gebel 0 742 Apr-03-2019, 07:59 PM
Last Post: gebel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020