随机数学计划

时间:2015-03-08 13:57:08

标签: python math

感谢您花时间阅读本文。 我必须创建一个程序,根据=, - 和*生成10个随机数学问题。我有程序可以工作,但每次我在主程序打印后运行它" none"即使这不在我的计划中。任何帮助都会非常感激。谢谢。

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

5 个答案:

答案 0 :(得分:1)

我认为您正在使用python 3。在python 3中,input与python 2中的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

答案 1 :(得分:1)

它正在打印None,因为print()函数返回None并且您将None的{​​{1}}值从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 s,但输入的答案是字符串。要正确地比较它们,它们需要是相同的类型。因此,您应该将输入的答案转换为int,或者将rightanswer转换为str

答案 2 :(得分:1)

使用input函数的方式有两个问题:

  • 您滥用prompt参数
  • 您忘记转换结果

首先,请更好地了解reference of the input function

提示参数

input将一个字符串作为参数显示("提示")给用户以指示程序正在等待输入。 print函数还向用户显示一个字符串,但它不会返回任何内容。它完成了它的全部工作(并且在Python中一个不返回任何内容的函数,返回None)。这是input显示的内容,因此显示None。你应该use format instead。它将格式化并返回输入可以显示的格式化字符串:

answer1_as_str=input("What is {} ?".format(question))))

answer2_as_str=input("What is {:d} - {:d} ?".format(Num1, Num2)))

返回值

input返回用户input as a string contrary to python 2(即完全按照输入的内容)。因此,如果需要,您必须将输入转换为所需类型。例如,如果您输入10,则输入将返回"10"。如果你需要一个int,你必须自己转换它。

answer1 = int(answer1_as_str)

答案 3 :(得分:1)

我不愿意给你一个答案就是为你做的,所以我会给你提供一些改进的提示。 (即这不是一个答案,只是一个评论太大 - 更像是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

这将为您完成大部分逻辑,并生成您想要的字符串,而不会复制所有其他代码。

这样的小改动可以使事情更具可读性。

答案 4 :(得分:0)

看起来你并不真正了解input()的工作原理。您可能还想查看不同的数据类型和条件语句。除此之外,这是一次非常好的尝试。这是我的解决方案:

from random import randint

print("Welcome")
name = input("What's your name?\n")
print("Nice to meet you " + name + ", you will be given 10 multiplication, addition and subtraction questions.")

for i in range(10):
    print("\nProblem " + str(i+1))

    num1 = randint(1,12)
    num2 = randint(1,12)
    sign = randint(1,3)
    if sign == 1:
        question = str(num1) + " + " + str(num2)
        answer = num1 + num2
    elif sign == 2:
        question = str(num1) + " - " + str(num2)
        answer = num1 - num2
    else:
        question = str(num1) + " x " + str(num2)
        answer = num1 * num2

    user_answer = input("What is " + question + "? ")
    if user_answer == str(answer):
        print("Well done!")
    else:
        print("Sorry, that's incorrect, the answer was", answer)