为什么输入名称后此代码无效?

时间:2015-07-19 08:52:27

标签: python

import random

name=input("What is your name?")
print ("Alright",name,"welcome to your maths quiz")
score=0

level_of_difficulty= input("What level of difficulty are you working at? Press 1 for low, 2 for intermediate or 3 for high")
if level_of_difficulty == 1:
    for question_num in range(1, 11):
            ops = ['+', '-', '*']
            number_1=random.randrange(1,10)
            number_2=random.randrange(1,10)
            operation = random.choice(ops)
            maths = eval(str(number_1) + operation + str(number_2))
            print('\nQuestion number: {}'.format(question_num))
            print ("The question is",number_1,operation,number_2)

            Answer=int(input ("What is your answer:"))
            if Answer==maths:
                print ("Correct")
                score=score+1
            else:
                print ("Incorrect. The actual answer is",maths)

            print("Well done you scored",score,"out of 10")
if level_of_difficulty == 2:
    for question_num in range(1, 11):
            ops = ['+', '-', '*']
            number_1=random.randrange(1,20)
            number_2=random.randrange(1,20)
            operation = random.choice(ops)
            maths = eval(str(number_1) + operation + str(number_2))
            print('\nQuestion number: {}'.format(question_num))
            print ("The question is",number_1,operation,number_2)

            Answer=int(input ("What is your answer:"))
            if Answer==maths:
                print ("Correct")
                score=score+1
            else:
                print ("Incorrect. The actual answer is",maths)

            print("Well done you scored",score,"out of 10")
if level_of_difficulty == 3:
    for question_num in range(1, 11):
            ops = ['+', '-', '*',"/"]
            number_1=random.randrange(1,20)
            number_2=random.randrange(1,20)
            operation = random.choice(ops)
            maths = eval(str(number_1) + operation + str(number_2))
            print('\nQuestion number: {}'.format(question_num))
            print ("The question is",number_1,operation,number_2)

            Answer=int(input ("What is your answer:"))
            if Answer==maths:
                print ("Correct")
                score=score+1
            else:
                print ("Incorrect. The actual answer is",maths)

    print("Well done you scored",score,"out of 10")

为什么这不起作用?没有无效的语法。所以我不知道为什么它不起作用。

3 个答案:

答案 0 :(得分:4)

你有

level_of_difficulty= input("What level of difficulty are you working at? Press 1 for low, 2 for intermediate or 3 for high")

if level_of_difficulty == 1:
    ...
if level_of_difficulty == 2:
    ...
if level_of_difficulty == 3:
    ...

但是input()会返回一个字符串,而不是int。通过更改

将输入转换为int
level_of_difficulty= input("What level of difficulty are you working at? Press 1 for low, 2 for intermediate or 3 for high")

level_of_difficulty= int(input("What level of difficulty are you working at? Press 1 for low, 2 for intermediate or 3 for high"))

答案 1 :(得分:1)

方法input()尝试将用户的输入作为代码执行。它相当于eval(raw_input(prompt))。在这里,您应该使用name = raw_input("What is your name?")

虽然您的问题非常清楚,我实际上并不知道您的问题是什么,但我清理了您的代码,这在Python 2中有效:

import random

name = raw_input("What is your name?\n")
print("Alright {0} welcome to your maths quiz".format(name))
score = 0

level_of_difficulty = input(("What level of difficulty are you working at?\n"
                             "Press 1 for low, 2 for intermediate "
                             "or 3 for high\n"))

if level_of_difficulty == 3:
    ops = ['+', '-', '*', '/']
else:
    ops = ['+', '-', '*']

for question_num in range(1, 11):
    if level_of_difficulty == 1:
        number_1 = random.randrange(1, 10)
        number_2 = random.randrange(1, 10)
    else:
        number_1 = random.randrange(1, 20)
        number_2 = random.randrange(1, 20)

    operation = random.choice(ops)
    maths = eval(str(number_1) + operation + str(number_2))
    print('\n*** Question {0} ***'.format(question_num))
    print("The question is {0} {1} {2}".format(number_1,
                                               operation,
                                               number_2))

    answer = int(raw_input("What is your answer: "))
    if answer == maths:
        print("Correct")
        score = score + 1
    else:
        print("Incorrect. The actual answer is {0}".format(maths))

print("Well done you scored {0} out of 10".format(score))

答案 2 :(得分:1)

您的代码在询问难度级别后才会运行。关键是输入是一个字符串或其他什么。因此,如果apple == orange,你可以让python比较苹果和橘子:这样做。那不起作用。

要解决此问题,您需要将输入转换为橙色,在这种情况下为整数。做这个广告int()' around'你的意见。像这样

level_of_difficulty= int(input("What level of difficulty are you working at? Press 1 for low, 2 for intermediate or 3 for high"))

goodluck