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


Messages In This Thread
How to write a response if a user inputs a string - by horuscope42 - Apr-28-2020, 09:58 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Multi set string inputs/outputs kwmcgreal 2 2,060 Sep-26-2020, 10:44 PM
Last Post: kwmcgreal
  Problem with accepting multiple string inputs Ryan_Todd 5 2,936 Jan-22-2020, 06:12 PM
Last Post: buran
  How to check if user entered string or integer or float?? prateek3 5 11,330 Dec-21-2019, 06:24 PM
Last Post: DreamingInsanity
  Perminantly saving user inputs + being able to retrieve it ThatOneGuyNoOneKnowsCodes 1 1,917 Oct-23-2019, 11:28 PM
Last Post: DT2000
  Trying to prompt user for 2 inputs on one line pythonprogrammer 2 2,497 Sep-15-2019, 04:41 PM
Last Post: snippsat
  Python start from a specific string line and write? searching1 1 2,212 Jun-27-2019, 02:28 PM
Last Post: perfringo
  write image into string format into text file venkat18 2 4,404 Jun-01-2019, 06:46 AM
Last Post: venkat18
  Using Pypdf2 write a string to a pdf file Pedroski55 6 20,261 Apr-11-2019, 11:10 PM
Last Post: snippsat
  user inputs in constructing a dictionary Exsul 3 3,698 Apr-10-2019, 12:25 PM
Last Post: ichabod801
  unit testing a method that asks two user inputs() in console gebel 0 2,141 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