如何重复 "input" 以获得正确答案?

How can I repeat an "input" in order to get a proper answer?

我正在制作一个小游戏。 这是我到目前为止所做的

World = input("Where do you live?")

x = "Ideas"
y = "Real"

def name(x):
    if World == x:
      return ('correct')
    elif World == y:
      return ('game over')
    else:
      return ('insert a valid answer')

print(name(World))

while name(x) != x and y:
      print(World)
      break

简而言之,我真的很想知道当输入不是 x 或 y 时如何重复输入。 我非常感谢任何形式的帮助。

试试这个:

World = input("Where do you live?")

x = "Ideas"
y = "Real"

while World != x and World != y:
    print('insert a valid answer')
    World = input("Where do you live?")
if World == x:
    print('correct')
else:
    print('game over')
resume = True
good = "Ideas"
bad = "Real"
while resume:
    i = input("Where do you live?")
    if i == good:
        print("correct")
        resume = False
    elif i == bad:
        print("game over")
        resume = False
    else:
        print("insert a valid answer")
print("code finished")

我稍微重写了代码,但这行得通:

def get_world():
    x, y = "Ideas", "Real" # In Python you can assign multiple values to multiple variables on a single line
    world = None

    while not (world == x or world == y):
        if world is not None: print("Insert a valid answer.") # The first time world == None, from the second loop onwards it will contain a string value
        world = input("Where do you live?  ")

    if world == x: print("Correct")
    elif world == y: print("Game over") # In this situation, this could also be an else statement

    return world

get_world()

如果你运行这个代码,你会得到这个:

Where do you live?  test
Insert a valid answer.
Where do you live?  Real
Game over

请注意,我添加了 return 声明。然后,您可以将所选世界分配给一个变量(如果需要),如下所示:

chosen_world = get_world()

试试这个:

def start_game():
World = input("Where do you live?")
x = "Ideas"
y = "Real"
while(World != x and World != y):
    World = input("insert a valid answer:")
if World == x:
    print('correct')
elif World == y:
    print('game over')

start_game()

首先我测试了部分再试一次

Where do you live?test
insert a valid answer:Ideas
correct

后来我测试错了

Where do you live?Real
game over