我的計算機科學類正在做一個練習編程任務,我們需要創建一個程序,要求提供十個隨機問題來測試學生的算術技能。我已經爲每個問題的兩個數字做了單獨的變量,一個用於隨機操作。這是我的代碼,我正在使用python 3.5,並且不斷收到錯誤。任何人都可以看到我出錯的地方......我認爲這是使用eval模塊。Python中的eval模塊出錯
#This is where the import commands go
from random import randint
import random
#This is a set list of variables for each question (21 in total as 2 for each question and operation)
operation = ['x', '-', '+']
q1p1 = (randint(0,100))
q1p2 = (randint(0,100))
#This variable stores the users score
score = 0
#This changes the randomly generated numbers into strings, then evauates them as maths equations so it knows the answers.
#Also it stores the whole of each question as a variable, making it easier to code.
question1 = eval(str(q1p1) + operation + str(q1p2))
#This asks for the user’s name
name = input("What is your forename (With No Caps?)")
surname = input("What is your surname (With No Caps?)")
#This prints a welcome message, personalised with the user's name
print("Welcome to your test," + name)
#Information about how to answer questions
print('''Throughout your test you will be asked a series of questions.
These questions will be randomly generated and are designed to test your basic arithmetic skills.
Please enter your answers in integer form.
EG. if the question was 5 + 5, you would enter 10 with no spaces or extra characters.''')
#First question
print (question1)
answer1 = input("What is the answer to the above question?")
if (question1) == True:
print("Well done, you got it correct!")
(score) + 1
後的誤差 – dahui
'操作= [ 'X', ' - ', '+']爲'操作= random.choice'變化( ['*',' - ','+'])'。其中返回一個字符串。在你的代碼嘗試附加列表。 –
感謝兄弟會在我回家的時候添加它! – joelittlejohns4