2016-05-06 74 views
-1

我的計算機科學類正在做一個練習編程任務,我們需要創建一個程序,要求提供十個隨機問題來測試學生的算術技能。我已經爲每個問題的兩個數字做了單獨的變量,一個用於隨機操作。這是我的代碼,我正在使用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 
+1

後的誤差 – dahui

+0

'操作= [ 'X', ' - ', '+']爲'操作= random.choice'變化( ['*',' - ','+'])'。其中返回一個字符串。在你的代碼嘗試附加列表。 –

+0

感謝兄弟會在我回家的時候添加它! – joelittlejohns4

回答

0

我跑的代碼,並得到了以下錯誤:

Can't convert 'list' object to str implicitly

爲線:

question1 = eval(str(q1p1) + operation + str(q1p2)) 

操作是一個列表,你想打印出的清單您的字符串中的操作。這不會是因爲你期望它的工作,你需要選擇一個單獨的操作,例如:

question1 = eval(str(q1p1) + str(operation[2]) + str(q1p2)) 

我不認爲,你期望它你的程序工作。你正在嘗試創建一個問題字符串,然後檢查這個「question1」是否爲真?以下是您的程序應執行的操作步驟:

  1. 從您的操作列表中選擇一項操作,但不能使用整個列表。
  2. 生成兩個隨機數(查找Python的「隨機」類)
  3. 檢查您從列表中選擇的操作,這個操作
  4. 執行兩個數字的計算從用戶獲取輸入(看啓動Python '輸入' 法)
  5. 檢查用戶輸入正確答案相匹配

    import random 
    
    operations = ['+','-','x'] 
    
    operation = random.choice(operations) 
    
    number1 = random.randrange(0,10) 
    number2 = random.randrange(0,10) 
    
    score = 0 
    
    answer = None 
    
    if operation == '+': 
        answer = number1 + number2 
    elif operation == '-': 
        answer = number1 - number2 
    elif operation == 'x': 
        answer = number1 * number2 
    
    user_answer = input("What is " + str(number1) + " " + operation + " " + str(number2) + "?") 
    user_answer = float(user_answer) 
    
    if user_answer == answer: 
        print("Correct") 
        score = score+1 
    

還行

score + 1 

不會做你認爲它所做的事情,它只是取得了分數,並增加了一個,它對此值沒有任何作用。爲了提高成績,你需要重新分配它放回比分:

score = score + 1 
+0

感謝您的快速回復,我沒有辦法生成隨機操作並將其插入數學問題,然後檢查用戶輸入是否正確? – joelittlejohns4

+0

再次檢查答案ive添加了一些代碼,可以完成你所要求的大部分功能。您需要自己修改它,但要在循環中執行此操作 – dahui

0

「x」是不是在蟒蛇的乘法運算符,使用「*」

調試時它是一個好主意,用打印來顯示代碼中有趣變量的值。

例如,當您在代碼中看到一行錯誤並且您不確定原因時。嘗試打印該行使用的值。

例如更改代碼以這樣的事情:

print('eval called with', str(q1p1) + operation + str(q1p2)) 
question1 = eval(str(q1p1) + operation + str(q1p2))