0
votes

Here is what I need coded:

  • Detect user input for the words: "add" and "sub" aka subtraction.
  • If the wrong answer is given, it should re-ask the question.
  • If the user chooses either, it should generate two numbers from 1-9, number 1 >= number 2, and the user should be asked the answer.
  • If the answer is correct, it should move to the next round (our teacher said there must be 5 rounds, at each end of the round the code should ask the user to type "add" or "sub" again.
  • If the user gives the wrong answer, it should re-ask the question.
  • The code ends when all 5 rounds are done, or if the user types "end" at any time with a message saying "Goodbye".

My code is:

import random

i = 1
while i < 6:
    answer1 = input("Hello, please type \"add\" for adding, or \"sub\" for subtracting")

    first_number = random.randrange(1, 9)
    second_number = random.randrange(1, first_number)
    one = str(first_number)
    two = str(second_number)

    if answer1 == "add":
        the_answer = first_number + second_number
        the_answer_actual = str(the_answer)
        user_answer = int(input(one + " + " + two + " = "))
        if the_answer == user_answer:
            print("The answer is correct!")
            i += 1
        elif the_answer != user_answer:
            print("Wrong answer. The correct answer was " + the_answer_actual)
            i += 1
        else:
            continue

    elif answer1 == "sub":
        the_answer = first_number - second_number
        the_answer_actual = str(the_answer)
        user_answer = int(input(one + " - " + two + " = "))
        if the_answer == user_answer:
            print("The answer is correct!")
            i += 1
        elif the_answer != user_answer:
            print("Wrong answer. The correct answer was " + the_answer_actual)
            i += 1
        else:
            continue

    elif answer1 == "end" or i < 6:
        print("Goodbye.")
        break

    else:
        pass

======================================================================== Problem: This error code appears when the 5th round is over:

  • Traceback (most recent call last): -File "C:\Users\kingh\PycharmProjects\pythonProject\main.py", line 8, in
    • second_number = random.randrange(1, first_number)
    • File "C:\Users\kingh\AppData\Local\Programs\Python\Python39\lib\random.py", line 316, in randrange
      • raise ValueError("empty range for randrange() (%d, %d, %d)" % (istart, istop, width)) ValueError: empty range for randrange() (1, 1, 0)
3
Generally speaking pass is used as a placeholder, continue might be more appropriate for the else clause. - TekkSparrow
Could you explicitly describe what isn't working? When asking questions, it's good practice to describe what your specific issue is, what the expected output is, and what has been attempted. - TekkSparrow
Ok so I know it is offtopic answer, but it would be good if you named your parameters better. instead of answer1 have something like function_selction, instead of answer2 have actual_solution, and instead of ans2 have actual_solution_string. It makes it less confusing for us to understand what is happening as we don't need to think 'wait was ans2 the string or the int, what even is ans2??'. It will also make it easier for you to work on your own code - Maritn Ge
It's done, I forgot that people might not know what I wrote xd - Sc1337
I did i =+ 1 instead of i += 1 Also, the code now properly works, with the only issue being that it doesn't stop after the 5th answer. - Sc1337

3 Answers

0
votes

You need to do this in both add and sub as it doesn't work in either:

if str(answer2) == useranswer:

or

useranswer = int(input("..."))

as else you will be comparing a string with an int which will never work. the first solution converts the number you calculated to a string, the second solution converts the input you get from the user to an int. Both work, but you cannot compare a String with an int.

If you want to do more comparisons with the userinput (like checking if the result was bigger/lower than what you entered) i would reccomend the first solution, as you will alwats have the userinput as an int until you compare it.

0
votes

You just need to convert the answer to an integer before comparing it :

if answer2 == int(user_answer):

and then it works perfectly (naturally you must do it for subtraction as well)

EDIT : and you need to add a break after the correct answer

print("The answer is correct!")
break
0
votes

You might need to use if ans2 == user_answer: instead of if answer2 == user_answer: since it is a string comparison.

EDIT: You might also want to use i += 1 instead to make sure it stops after the specified number of iterations.