2016-04-23 86 views
-6

我的代碼應該只接受字母(例如,傑克將被接受,並且jack1不會被接受)。只接受字母作爲輸入

它提示用戶他們的第一個和最後一個名稱,然後將它們存儲。一旦我編寫了第一個名字的代碼,我就測試了它是否爲第一個名字正確編寫了代碼,但它一直給我this error

的答案可以請你告訴如何使只有數字這項工作被允許?

代碼

import random 

operators = ["+", "-", "*"] 


def greeting(first_name, last_name): 
    print ("Hello", first_name + " " + last_name) 
    Play = input('Are you ready to begin?') 
    if Play == 'yes': 
    print("Great", first_name + ", lets begin") 
    else: 
    greeting(first_name, last_name) 



def Players_input(): 
    print ("Welcome to the Arithmetic Quiz") 
    first_name = input("Please enter your first name: ") 
    if all(x.isalpha() or x.isspace() for x in first_name): 
    last_name = input("Please enter your last name: ") 
    greeting(first_name, last_name) 
    else: 
    print("Only alphabetical letters and spaces: no") 

Players_input() 

score = 0               
for i in range(10):        
    first_number = random.randint(1,12)   
    second_number = random.randint(1,12)     
    op = random.choice(operators) 


    print (first_number, op, second_number, " = ?") 
    users_answer = int(input()) 

    if op == "+": 
     right_answer = first_number + second_number 
    elif op == "-": 
     right_answer = first_number - second_number 
    elif op == "*":    
     right_answer = first_number * second_number  

    if users_answer == right_answer: 
     print("Well Done!") 
     score += 1 
    else: 
     print ("Sorry but thats the wrong answer, the right answer is: " + str(right_answer) + ". Better luck next time") 

print (first_name, "Your final score in the Arithmetic Quiz is", str(score), "out of 10") 
+3

你不應該說「請幫忙」三次,然後徑直往代碼,並說「修正錯誤」。你需要提出一個連貫的問題以獲得連貫的答案。 –

+0

難道你不明白'first_name'是'Players_input'函數的局部變量嗎? – ForceBru

+1

請不要將您的錯誤發佈爲圖片。使用代碼格式:) – Signal

回答

1

對於第一個問題的答案:

你從未定義FIRST_NAME Players_input。這個值只存儲在函數內部,之後get被刪除。 (更多關於這the link added by gjttt1) 有兩種方法來解決這個問題:

  • 你可以讓FIRST_NAME全球。但這是一種糟糕的風格,所以我不會使用這個選項。你會在某個時候在Players_input添加global first_name,之前它被寫入(所以之前或直接在第一印刷呼叫後)
  • 你可以返回FIRST_NAME,這是首選的方式。在Players_input的末尾添加return first_name,並用first_name = Players_input()替換Players_input()

回答了第二個問題:

就用它代替int(input())這個函數(int_input()替換此行):

def int_input(prompt="", error_message="You didn't enter an integer!"): 
    while True: # repeat this until the function returns 
     inp = input(prompt) # get the input after autputting the prompt. 
     try: # Try to... 
      return int(inp) # turn it into an integer. If it works, return it. 
     except ValueError: # If it didn't work, it raised a ValueError. In this case... 
      if error_message: # print the error_message if it is not "", false or None. 
       print(error_message) 

然後你有第三個問題:你應該只使用函數名稱中的小寫字母,以區分它們與類。這只是你的風格,但它肯定有助於開發出一種好的,清晰的編碼風格。

我希望我能幫上忙,

CodenameLambda

+0

感謝CodingLambdas,我已經讓我的代碼工作並做我想做的事情。 :) – IcarusFlight

+0

不客氣。 – CodenameLambda

1

查看錯誤。它告訴你first_name變量沒有被定義。這是因爲它是Players_input函數中的局部變量,不能在其他地方使用。在函數內部定義的變量放在內存中的堆棧中,並在該堆棧框架被推出堆棧時被銷燬。你稱之爲「超出範圍」。

我建議您查看有關變量範圍的信息。

0

你可以聲明FIRST_NAME =(「你好」)前面的代碼,然後當你把它作爲一個輸入功能,你可以寫這樣的:

first_name=str(input('Please enter your first name: '))

+1

將類型更改爲字符串不會* nothing *(因爲input * already *返回一個字符串),並且* nothing *來解決問題,變量仍然超出範圍。括號也是多餘的。你真的*應該用''''包圍代碼,因爲它不會以代碼形式顯示。 – CodenameLambda