Python 只接受字母作为输入

Python 只接受字母作为输入,python,python-3.x,Python,Python 3.x,我的代码应该只接受字母(例如Jack将被接受,jack1将不被接受) 它会提示用户输入他们的名字和姓氏,然后将其存储。一旦我为第一个名字写了代码,我就测试它,看看我是否为第一个名字写了正确的代码,但它一直给我答案 在答案中,你能说明如何在只允许数字的情况下实现这一点吗 代码 import random operators = ["+", "-", "*"] def greeting(first_name, last_name): print ("Hello", first_name +

我的代码应该只接受字母(例如Jack将被接受,jack1将不被接受)

它会提示用户输入他们的名字和姓氏,然后将其存储。一旦我为第一个名字写了代码,我就测试它,看看我是否为第一个名字写了正确的代码,但它一直给我答案

在答案中,你能说明如何在只允许数字的情况下实现这一点吗

代码

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")

尝试使用第一个变量时,该变量超出范围。它属于Players_input()函数


查看错误。它告诉您,
first\u name
变量未定义。这是因为它是
Players\u input
函数中的局部变量,不能在其他地方使用。函数中定义的变量放在内存中的堆栈上,当堆栈帧从堆栈中推出时,这些变量将被销毁。你称之为“超出范围”


我建议您查找有关变量范围的信息。

第一个问题的答案:

您从未在
Players\u input
之外定义过名字。这个值只是存储在函数中,然后get被删除。(更多信息请参阅) 有两种方法可以解决此问题:

  • 你可以让名字成为全球性的。但这是一种糟糕的风格,所以我不会使用这个选项。在写入之前(在第一次打印调用之前或之后),您可以在
    Players\u input
    的某个位置添加
    global first\u name
  • 您可以返回first_name,这是首选方式。在
    Players\u input
    末尾添加
    return first\u name
    ,并将
    Players\u input()
    替换为
    first\u name=Players\u input()
第二个问题的答案是:

只需使用此函数而不是
int(input())
(将此行替换为
int\u input()
):

def int_input(prompt=”“,error_message=“您没有输入整数!”):
while True:#重复此操作直到函数返回
inp=input(prompt)#在输入提示后获取输入。
尝试:#尝试。。。
返回int(inp)#将其转换为整数。如果有效,请退回。
ValueError除外:#如果它不起作用,则会引发ValueError。在这种情况下。。。
if error_message:#如果错误消息不是“”、false或None,则打印错误消息。
打印(错误消息)
然后还有第三个问题:应该在函数名中使用小写字母,以将它们与类区分开来。这只是你的风格,但它肯定会有助于开发一个好的、清晰的编码风格

我希望我能帮上忙


CodenameLambda

您可以在代码前面声明first_name=('hi'),然后当您将其作为输入函数调用时,您可以编写如下代码:


first\u name=str(输入('请输入您的名字:')

您不应该说“请帮助”三次,然后转储代码并说“修复错误”。你需要提出一个连贯的问题才能得到连贯的答案。难道你不明白
first\u name
Players\u input
函数的一个局部变量吗?请不要将你的错误作为图片发布。使用代码格式:)谢谢编写Lambdas,我已经让代码正常工作,并且做了我想做的事情。:)将类型更改为字符串不会产生任何效果(因为输入已经返回字符串),也不会解决问题,变量仍然超出范围。括号也是多余的。你真的应该用
`
来包围代码,因为它不是以代码的形式显示的。