随机数学程序

2024-06-11 15:52:56 发布

您现在位置:Python中文网/ 问答频道 /正文

谢谢你抽出时间来读这篇文章。 我必须创建一个程序,根据=,-和*生成10个随机数学问题。我有程序工作,但每次我运行后,它打印“无”主程序,即使这不是在我的有什么计划吗如果能帮上忙,我将不胜感激。非常感谢。你知道吗

import random 
print ("Welcome")
name=input("What's your name?")
print("Nice to meet you", name, ",you will be given 10 multiplication, addition and subtraction questions.")
Num1 = random.randint(1,12) 
Num2 = random.randint(1,12) 
sign = random.randint(1,3) 
if sign == 1: # If the random  number generated is 1
  question = Num1 + Num2 
  rightanswer1 = Num1 + Num2  
  answer1=input(print("What is", question ,"?"))
  if answer1 == rightanswer1: 
    print("Well Done!")
  if answer1 != rightanswer1: 
    print("Sorry, that's incorrect, the answer was", rightanswer1) 
if sign == 2: 
  question = Num1 - Num2 
  rightanswer2 = Num1 - Num2
  answer2=input(print("What is", Num1, "-", Num2 ,"?"))
  if answer2 == rightanswer2:
    print("Well done!")
  elif answer2 != rightanswer2:
    print("Sorry, that's incorrect, the answer was", rightanswer2)
if sign == 3:
  question = Num1 * Num2
  rightanswer3 = Num1 * Num2
  answer3=input(print("What is", Num1, "x", Num2 ,"?"))
  if answer3 == rightanswer3:
    print("Well done!")
  elif answer3 != rightanswer3:
    print("Sorry, that's incorrect, the answer was", rightanswer3)`
> Welcome
> What's your name? John
> Nice to meet you John ,you will be given 10 multiplication, addition and subtraction questions.
> What is 12 x 3 ?
> None 36
> Sorry, that's incorrect, the answer was 36

Tags: thenameyouinputifisrandomwhat
3条回答

我不愿意只给你一个对你有用的答案,所以我会给你一些改进的提示。(也就是说,这不是一个答案,只是一个太大的评论——更像是一个codereview答案)

首先,你使用这样的结构:

if x == 1:
  #do something

if x == 2:
  #do something else

...

在这种情况下,如果按预期使用if语法,阅读起来就更容易了,这没有什么区别:

if <condition>:
  #do this if the above test is true.
elif <more conditions>:
  #do this only if the first test is false and this one is true
elif <more conditions>:
  #same as above, except for the second test must be false too
else:
  #do this if all the above tests are false

所以你可以这样用:

if sign == 1:
  ...
elif sign == 2:
  ...
elif sign == 3:
  ...
else:
  # something weird happened...

这将使程序的这一部分更容易理解。你知道吗

同样的事情也可以在if answer1 == rightanswer1:节中完成

if answer1 == rightanswer1:
  #correct!
else:
  #incorrect.

那就更清楚了。您似乎已经在其中的几个中使用了if...elif样式,但不是第一个。你知道吗

一旦你有了这个,它会更清晰一点。你知道吗

下一种改进方法是删除重复的代码。您不需要为每个标志分别设置分支,只需将所有标志合并为一个:

number1 = randint(1,12)
number2 = randint(1,12)

op = randint(1,3)

question_string = "%d %s %d = ?" % (number1, number2, ["+", "-", "*"][op])

result = None
if op == 1:
  result = number1 + number2
elif op == 2:
  result = number1 - number2
elif op == 3:
  result = number1 * number2

这将为您完成大部分逻辑,并生成所需的字符串,而无需复制所有其他代码。你知道吗

像这样的小改动可以使事情更具可读性。你知道吗

我想你在用python 3。在python3中input类似于python2中的raw_input。所以你得到字符串作为输入。所以把它转换成int

var = int(input("Enter a number: "))

所以在你的代码中

print("What is", Num1, "x", Num2 ,"?")
answer3 = input()
answer3 = int(answer3) 

看这个:

whats-the-difference-between-raw-input-and-input-in-python3-x

它正在打印None,因为print()函数返回None,而您将None的值从print()传递给input()函数作为提示。例如

answer3=input(print("What is", Num1, "x", Num2 ,"?"))

因此print("What is", Num1, "x", Num2 ,"?")打印其内容,并返回None,然后input()将其作为提示打印出来。你知道吗

解决这个问题的一个简单方法是将print()函数调用移出input()函数。你知道吗

例如

print("What is", Num1, "x", Num2 ,"?")
answer3=input()

但是,您的程序还有另一个主要的问题:rightanswer变量是int,但输入的答案是字符串。为了正确地比较它们,它们必须是同一类型的。所以你要么把输入的答案转换成int,要么把rightanswer转换成str。你知道吗

相关问题 更多 >