我的代碼應該只接受字母(例如,傑克將被接受,並且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")
你不應該說「請幫忙」三次,然後徑直往代碼,並說「修正錯誤」。你需要提出一個連貫的問題以獲得連貫的答案。 –
難道你不明白'first_name'是'Players_input'函數的局部變量嗎? – ForceBru
請不要將您的錯誤發佈爲圖片。使用代碼格式:) – Signal