字母内部" int(输入(())"

时间:2015-03-20 12:06:15

标签: python numbers integer

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行时。

2 个答案:

答案 0 :(得分:1)

使用例外来捕获意外的输入。

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

答案 1 :(得分:0)

当您转换为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)

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