在Python中,如何让用户输入字符而不是数字

jk9hmnmh  于 2022-11-26  发布在  Python
关注(0)|答案(1)|浏览(281)

我正在创建一个简单的交互式故事游戏,我要求用户输入并向他们显示一些句子。我想做的是询问用户这些句子来自哪里,以及他们是否输入了int而不是string。我想在用户不输入int的情况下反复询问它们来自哪里。我已经尝试了if和while循环,但似乎不起作用

import time

name = input("what is your name? ")

def greeting (name):
    print("hhhmmm..... is that so?")
    time.sleep(0)
    x = input("Well " + name + " where have you been all these years?")
    place_char = ""
    while True:
        if x == place_char:
            break
            # I want to keep asking for location until the user put a string or char not a number
        print("please enter a location ")
    print("So " + name + " you're telling me that you've been at " + x + " this entir time?")

greeting(name)
kx1ctssn

kx1ctssn1#

一种方法是使用isalpha()返回True,如果字母表中的所有字符:

x.isalpha()

如果返回False,则请求新的输入并继续while循环。

while x.isalpha() == False:
    x = input("please enter a location ")

相关问题