需要有关以下python 3代码的一些见解

时间:2018-07-30 17:58:06

标签: python-3.x

我试图以有趣的方式构建此基本代码,但遇到一些奇怪的错误。以下代码要求提供基本的打印输出方程式的答案。当答案为10或更大时,它的行为很奇怪。我尝试实现一些错误处理方法(我猜是int(ansr_raw)失败)。我认为更好的方法会帮助我。您无需为我拼写代码,但将我的方向指示正确会有所帮助。代码和输出如下。

代码-

import random

signs = ['+', '-']
verbals = ['out', 'quit', 'help', 'break']
while True:
    a, b = random.randint(1, 9), random.randint(1, 9)
    ch = random.choice(signs)
    print("{} {} {} = ?".format(a, ch, b))
    ansr_raw = input("Enter the answer: ")

    if ansr_raw in '0123456789':        # trying to handle error
        ansr = int(ansr_raw)
    else:
        for i in verbals:
            if ansr_raw == i:
                choice = input("Wish to Quit? [y/n] ").lower()
                if choice in 'yes':
                    print("Quit Successful.")
                    break
                elif choice in 'no':
                    continue
                else:
                    print("Wrong choice. continuing game.")
                    continue
        print('answer format invalid')
        continue

    if ch == '+':
        if ansr == (a + b):
            print("Right Answer.")
        else:
            print("wrong answer.")
    elif ch == '-':
        if ansr in ((a - b), (b - a)):
            print("Right Answer.")
        else:
            print("wrong answer.")

以下是输出(已添加箭头标记)-

9 + 3 = ?
Enter the answer:  12
answer format invalid    <----
2 - 9 = ?
Enter the answer:  5
wrong answer.
1 - 3 = ?
Enter the answer:  2
Right Answer.
8 + 3 = ?
Enter the answer:  11
answer format invalid    <----
1 + 2 = ?
Enter the answer:  3
Right Answer. 
6 + 2 = ?
Enter the answer: 

1 个答案:

答案 0 :(得分:0)

问题在于你在说什么

if ansr_raw in '0123456789':        # trying to handle error
        ansr = int(ansr_raw)

对于像9这样的单个数字,是的,在该字符串中有一个9,但是只有两个数字(01,23,34,45,56,67,78,89)重新尝试做,检查字符串是否可以成为整数;做这个。

try:
    ansr = int(ansr_raw)
except ValueError:

完整代码:

import random
import sys
signs = ['+', '-']
verbals = ['out', 'quit', 'help', 'break']

running = True
while running:
    a, b = random.randint(1, 9), random.randint(1, 9)
    ch = random.choice(signs)
    print("{} {} {} = ?".format(a, ch, b))
    ansr_raw = input("Enter the answer: ")
    try:
        ansr = int(ansr_raw)
    except ValueError:
        answerInv = True
        for i in verbals:
            if ansr_raw == i:
                choice = input("Wish to Quit? [y/n] ").lower()
                if choice in 'yes':
                    print("Quit Successful.")
                    sys.exit(0)
                elif choice in 'no':
                    continue
                    answerInv = False
                else:
                    print("Wrong choice. continuing game.")
                    answerInv = False
                    continue
        if answerInv:
            print('answer format invalid')

    if ch == '+':
        if ansr == (a + b):
            print("Right Answer.")
        else:
            print("wrong answer.")
    elif ch == '-':
        if ansr in ((a - b), (b - a)):
            print("Right Answer.")
        else:
            print("wrong answer.")