0
def multiply():                                                                                     #starts sub program when 'multiply()' is called
num1 = random.randint(1,12)                                                                     #randomly generates a number between 1 and 12
num2 = random.randint(1,12)
while loop == True:                                                                             #creates loop, and uses previously defined 'loop'
    ans = int(input("What is the answer to " + str(num1) + " x " + str(num2) + " ? "))          #asks question and requires a user input
    correct = (ans == num1 * num2)
    if correct:
        print("You are correct! ")
        break                                                                                   #if the answer is correct, it prints 'You are correct!' and breaks to avoid the loop
    else:
        print("Wrong, please try again. ")
    loop == False                                                                               #if the answer is wrong, it loops back to when 'loop' was last 'True'

I am wondering if there is a way for me to include a line of code that allows me to display "That is not an option!" when a symbol other than a number is entered into the 5th line in the code.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Ryan Jones
  • 75
  • 1
  • 5
  • 1
    @user5061 Due to the syntax of their `print` statements and use of `input` instead of `raw_input`, I'm going to assume 3.x – Cory Kramer Mar 20 '15 at 12:13

2 Answers2

1

Use an exception to catch unexpected inputs.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import random

def multiply():

    # Randomly generates a number between 1 and 12
    num1 = random.randint(1,12)
    num2 = random.randint(1,12)

    while True:

        i = input("What is the answer to {} x {} ".format(
            str(num1), str(num2)))

        try:
            ans = int(i)
        except ValueError:
            print('That is not an option!')
            continue

        if ans == num1 * num2:
            print("You are correct!")
            break
        else:
            print("Wrong, please try again.")

if __name__ == "__main__":

    multiply()
Jérôme
  • 13,328
  • 7
  • 56
  • 106
0

When you convert to int there is the chance that they will enter a non-integer value so the conversion will fail, so you can use a try/except

def multiply():                                                                                     #starts sub program when 'multiply()' is called
    num1 = random.randint(1,12)                                                                     #randomly generates a number between 1 and 12
    num2 = random.randint(1,12)
    while loop == True:                                                                             #creates loop, and uses previously defined 'loop'
        try:
            ans = int(input("What is the answer to " + str(num1) + " x " + str(num2) + " ? "))          #asks question and requires a user input
            correct = (ans == num1 * num2)
            if correct:
                print("You are correct! ")
                break                                                                                   #if the answer is correct, it prints 'You are correct!' and breaks to avoid the loop
            else:
                print("Wrong, please try again. ")
            loop == False   
        except ValueError:
            print("That is not an option")

Note that your previous code is now nested in a try block. If the int() fails because they entered a bad input, it will throw a ValueError that you can catch and notify them.

As a side note, another way to format your question to them would be

'What is the answer to {} x {}?'.format(num1, num2)

This is a nice way to generate a string with injected variable values.

Cory Kramer
  • 114,268
  • 16
  • 167
  • 218