询问用户是否要再次开始测验或在回答5个问题后退出测验

时间:2017-12-28 12:06:04

标签: python-3.x

除重启部分外,所有代码都很好。这部分的目的是询问用户是否要再次进行测验,或者在回答5个问题后退出测验,但我不能这样做。

  import random
**restart == "yes"
while restart == "yes":**------this is the main part of the problem.whenever i run the program it says that restart is not defined and i just get even more confused.please,please help.
 print("1.Addition")
 print("2.Subtraction")
 print("3.Multiplication")
 print("4.Division")
 print("5.exit")
 score = 0 
 choice1=int(input("please select an option by entering the number\n"))
 if choice1 == 1:
     num1 = random.randint(1,5)
     num2 = random.randint(1,5)
     print ("what is",num1,"+",num2)
     answer = num1 + num2
     q1 = int(input(""))
     if q1 == answer:
         print ("good")
         score = score + 1
     if q1 != answer:
         print("incorrect")
     num1 = random.randint(1,5)
     num2 = random.randint(1,5)
     print ("what is",num1,"+",num2)
     answer = num1 + num2
     q1 = int(input(""))
     if q1 == answer:
         print ("good")
         score =score + 1
     if q1 != answer:
         print("incorrect")
     num1 = random.randint(1,5)
     num2 = random.randint(1,5)
     print ("what is",num1,"+",num2)
     answer = num1 + num2
     q1 = int(input(""))
     if q1 == answer:
         print ("good")
         score = score +1
     if q1 != answer:
         print("incorrect")
     num1 = random.randint(1,5)
     num2 = random.randint(1,5)
     print ("what is",num1,"+",num2)
     answer = num1 + num2
     q1 = int(input(""))
     if q1 == answer:
         print ("good")
         score = score + 1
     if q1 != answer:
        score = score + 1
        print("incorrect")
     num1 = random.randint(1,5)
     num2 = random.randint(1,5)
     print ("what is",num1,"+",num2)
     answer = num1 + num2
     q1 = int(input(""))
     if q1 == answer:
         print ("good")
         score = score + 1
         print(score)
     if q1 != answer:
         print("incorrect")
     print("your score out of 5 is")
     print(score)
 if choice1 == 2:
     num1 = random.randint(1,5)
     num2 = random.randint(1,5)
     print ("what is",num1,"-",num2)
     answer = num1 - num2
     q1 = int(input(""))
     if q1 == answer:
         print ("good")
         score = score + 1
     if q1 != answer:
         print("incorrect")
     num1 = random.randint(1,5)
     num2 = random.randint(1,5)
     print ("what is",num1,"-",num2)
     answer = num1 - num2
     q1 = int(input(""))
     if q1 == answer:
         print ("good")
         score =score + 1
     if q1 != answer:
         print("incorrect")
     num1 = random.randint(1,5)
     num2 = random.randint(1,5)
     print ("what is",num1,"-",num2)
     answer = num1 - num2
     q1 = int(input(""))
     if q1 == answer:
         print ("good")
         score = score + 1
     if q1 != answer:
         print("incorrect")
     num1 = random.randint(1,5)
     num2 = random.randint(1,5)
     print ("what is",num1,"-",num2)
     answer = num1 - num2
     q1 = int(input(""))
     if q1 == answer:
         print ("good")
         score = score + 1
     if q1 != answer:
         print("incorrect")
     num1 = random.randint(1,5)
     num2 = random.randint(1,5)
     print ("what is",num1,"-",num2)
     answer = num1 - num2
     q1 = int(input(""))
     if q1 == answer:
         print ("good")
         score = score + 1
         print(score)
     if q1 != answer:
         print("incorrect")
     print("your score out of 5 is"/n)
     print(score)
 if choice1 == 3:
     num1 = random.randint(1,5)
     num2 = random.randint(1,5)
     print ("what is",num1,"*",num2)
     answer = num1 + num2
     q1 = int(input(""))
     if q1 == answer:
         print ("good")
         score = score + 1
     if q1 != answer:
         print("incorrect")
     num1 = random.randint(1,5)
     num2 = random.randint(1,5)
     print ("what is",num1,"*",num2)
     answer = num1 + num2
     q1 = int(input(""))
     if q1 == answer:
         print ("good")
         score =score + 1
     if q1 != answer:
         print("incorrect")
     num1 = random.randint(1,5)
     num2 = random.randint(1,5)
     print ("what is",num1,"*",num2)
     answer = num1 + num2
     q1 = int(input(""))
     if q1 == answer:
         print ("good")
         score = score +1
     if q1 != answer:
         print("incorrect")
     num1 = random.randint(1,5)
     num2 = random.randint(1,5)
     print ("what is",num1,"*",num2)
     answer = num1 + num2
     q1 = int(input(""))
     if q1 == answer:
         print ("good")
         score = score + 1
     if q1 != answer:
        score = score + 1
        print("incorrect")
     num1 = random.randint(1,5)
     num2 = random.randint(1,5)
     print ("what is",num1,"*",num2)
     answer = num1 + num2
     q1 = int(input(""))
     if q1 == answer:
         print ("good")
         score = score + 1
         print(score)
     if q1 != answer:
         print("incorrect")
     print("your score out of 5 is",score)
 if choice1 == 4:
     num1 = random.randint(1,5)
     num2 = random.randint(1,5)
     print ("what is",num1,"/",num2)
     answer = num1 - num2
     q1 = int(input(""))
     if q1 == answer:
         print ("good")
         score = score + 1
     if q1 != answer:
         print("incorrect")
     num1 = random.randint(1,5)
     num2 = random.randint(1,5)
     print ("what is",num1,"/",num2)
     answer = num1 - num2
     q1 = int(input(""))
     if q1 == answer:
         print ("good")
         score =score + 1
     if q1 != answer:
         print("incorrect")
     num1 = random.randint(1,5)
     num2 = random.randint(1,5)
     print ("what is",num1,"/",num2)
     answer = num1 - num2
     q1 = int(input(""))
     if q1 == answer:
         print ("good")
         score = score + 1
     if q1 != answer:
         print("incorrect")
     num1 = random.randint(1,5)
     num2 = random.randint(1,5)
     print ("what is",num1,"/",num2)
     answer = num1 - num2
     q1 = int(input(""))
     if q1 == answer:
         print ("good")
         score = score + 1
     if q1 != answer:
         print("incorrect")
     num1 = random.randint(1,5)
     num2 = random.randint(1,5)
     print ("what is",num1,"/",num2)
     answer = num1 - num2
     q1 = int(input(""))
     if q1 == answer:
         print ("good")
         score = score + 1
         print(score)
     if q1 != answer:
         print("incorrect")
     print("your score out of 5 is"/n)
     print(score)
 if choice1 == 5:
     print("bye,bye")
     exit()

重新启动=输入("您想再次开始测验吗?是吗?否?")     如果重启=="没有":         退出 -----这是问题的一部分,也是下面的问题。

1 个答案:

答案 0 :(得分:1)

看一下restart的定义:

restart == "yes" # incorrect!

这是正确的:

restart = "yes" #correct
#rest of the code

您应该使用\n代替/n新行