2015-07-19 48 views
-2
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") 

爲什麼這不起作用?沒有無效的語法。所以我不知道爲什麼它不起作用。爲什麼在我輸入名字後這段代碼不起作用?

+1

什麼Python版本您使用的? –

+1

'爲什麼這不起作用,沒有無效的語法,所以我不知道爲什麼它不能正常工作' - 您將需要更多的信息來吸引更多的幫助。 SO不是大量代碼的個人調試服務。 – Sinkingpoint

+0

我正在使用pyscripter,但我認爲它的3.1「 – Ibrahim

回答

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。通過改變

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

將輸入到int

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

方法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)) 
+1

我相信OP是使用python3 –

+0

儘管OP確實說它似乎通過python3 *聳肩* – mjalkio

+1

過去的名稱在回覆mjalkio非常感謝,但您的程序使用相同的數字爲每個問題是不是期望的結果 – Ibrahim

1

您的代碼doenst要求一個難度級別後運行。關鍵是輸入是一個字符串或其他。所以你問python比較蘋果和橙子,如果蘋果==橙子:做到這一點。那不起作用。

要解決此問題,您需要將輸入轉換爲橙色,在這種情況下爲整數。做這個廣告int()'圍繞'你的輸入。這樣

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

古德勒克

相關問題