gpt4 book ai didi

python-3.x - Python加法数学测验

转载 作者:行者123 更新时间:2023-12-03 08:05:18 24 4
gpt4 key购买 nike

只是学习python,我试图做一个非常简单的数学测验,但运行时出现语法错误,请解释我做错了什么

from random import randint
inf = 0
while inf < 10:
num1 = randint(0,5000)
num2 = randint(0,5000)
ans = num1+num2
print(num1,"+",num2)
plrans = input(int()"What's the answer?")
if plrans = ans
print("Correct!")
else
print("Incorrect :(")

最佳答案

这里有些错误:

  • 在从用户
  • 获得输入后,应完成到 int的转换。
  • =是赋值运算符。为了检查是否相等,您应该使用==运算符
  • 您的if语句缺少冒号(:)
  • 您的else语句也是如此:

  • from random import randint
    inf = 0
    while inf < 10:
    num1 = randint(0,5000)
    num2 = randint(0,5000)
    ans = num1+num2
    print(num1,"+",num2)
    plrans = int(input("What's the answer?")) # issue 1
    if plrans == ans : # issues 2 and 3
    print("Correct!")
    else: # issue 4
    print("Incorrect :(")

    关于python-3.x - Python加法数学测验,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35660450/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com