Python输入只接受一个argumen

2024-04-29 09:59:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用python3.3.4编写一个文本游戏,有一次我要一个名字。是否有一种方法可以只接受一个名称(输入的一个参数)以及如果用户输入多个参数。 这是我目前所拥有的。在

name =input('Piggy: What is your name?\n').title()
time.sleep(1)
print('Hello, {}. Piggy is what they call me.'.format(name))
time.sleep(1)
print('{}: Nice to meet you'.format(name))
time.sleep(1)
print('** Objective Two Completed **')

我想我需要使用while,then if和elif之类的东西。 非常感谢帮助


Tags: 方法用户name文本名称format游戏input
1条回答
网友
1楼 · 发布于 2024-04-29 09:59:27
while True:
    name = input("What is your name? ").strip()
    if len(name.split()) == 1:
        name = name.title()
        break
    else:
        print("Too long! Make it shorter!")

相关问题 更多 >