里面的字母 "int(input(())"

Letters inside "int(input(())"

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'

我想知道是否有办法让我包含一行代码,当在代码的第 5 行中输入数字以外的符号时,允许我显示 "That is not an option!"。

当你转换为int时,他们可能会输入一个非整数值,因此转换会失败,所以你可以使用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")

请注意,您之前的代码现在嵌套在 try 块中。如果 int() 因为他们输入了错误的输入而失败,它会抛出一个 ValueError,你可以捕获并通知他们。

作为旁注,另一种向他们提出问题的方式是

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

这是生成带有注入变量值的字符串的好方法。

使用异常捕获意外输入。

#!/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()