因为有多个IF语句,所以问题被多次提出 – 循环永远不会关闭。

huangapple go评论60阅读模式
英文:

Input question being asked multiple times because of multiple IF statements - Loop never closes(edited)

问题

我是新手学习Python,试图理解所有部分如何协同工作。我尝试编写一个脚本,根据用户的输入打印不同的消息。因为我将问题定义在一个函数中,我发现它会在每个if语句中打印,直到找到正确的答案。我想知道如何只打印一次问题,然后根据用户的输入显示正确的行。

使用变量,如果我输入“4”,循环永远不会关闭。

def quest2():
    q2 = input("\n你喜欢什么饮料? \n\t1. 啤酒 \n\t2. 葡萄酒 \n\t3. 伏特加 \n\t4. 我不喝酒(回答:1、2、3或4)")
    return q2

variable1 = quest2()

while True:

    if variable1 == "1":
        print("\n你选择了安全路线")
        break
    if variable1 == "2":
        print("\n有人有很好的品味")
        break
    if variable1 == "3":
        print("\n你喜欢自己调制饮料")
        break
    else:
        print("\n别撒谎,每个人都喝酒,我会给你另一次机会改变你的答案")
        continue
英文:

I'm new into python and I'm trying to understand how everything works together. I'm trying to write a script that prints different messages depending on the user input. because I defined the question in a function, I figured that it gets printed for each if statement until it reaches the correct answer. I was wondering how can I do this to print the question only once and then, based on the user input, to show the correct line.

Using the variable, the loop never closes if I input "4"

def quest2():
    q2 = input("\nWhat's your prefered beverage? \n\t1. Beer \n\t2. Wine \n\t3. Vodka \n\t4. I don't drink alcohool (Answer with: 1, 2, 3 or 4)")
    return q2

variable1 = quest2()

while True:

    if variable1 == "1":
        print("\nYou went the safe route")
        break
    if variable1 == "2":
        print("\nSomeone has some fine taste")
        break
    if variable1 == "3":
        print("\nYou build your own stuff")
        break
    else:
        print("\nStop lying, everyone drinks, I'll give you another chance to change your answer")
        continue

答案1

得分: 0

def quest2():
    q2 = input(
        "\n你喜欢什么饮料?\n\t1. 啤酒\n\t2. 葡萄酒\n\t3. 伏特加\n\t4. 我不喝酒(请用数字 1、2、3 或 4 回答)")
    return q2

while True:
    question = quest2() # 在这里调用一次问题并为以后检查分配结果。
    if question == "1": # 无需再调用另一个问题。
        print("\n你选择了安全的路线")
        break
    if question == "2": # 无需再调用另一个问题。
        print("\n有人品味不错")
        break
    if question == "3": # 无需再调用另一个问题。
        print("\n你自己调制饮料")
        break
    if question == "4":
        print("\n好的")
        break
    else:
        print("\n别撒谎,每个人都喝酒,我会给你改变答案的机会")
        continue
英文:

You just have to assign value for the question, then use that value.

def quest2():
    q2 = input(
        "\nWhat's your prefered beverage? \n\t1. Beer \n\t2. Wine \n\t3. Vodka \n\t4. I don't drink alcohool (Answer with: 1, 2, 3 or 4)")
    return q2

while True:
    question = quest2() # Call question once here and assign result for it for later check.
    if question == "1": # No need to call another question.
        print("\nYou went the safe route")
        break
    if question == "2": # No need to call another question.
        print("\nSomeone has some fine taste")
        break
    if question == "3": # No need to call another question.
        print("\nYou build your own stuff")
        break
    if question == "4":
        print("\nOk")
        break
    else:
        print("\nStop lying, everyone drinks, I'll give you another chance to change your answer")
        continue

huangapple
  • 本文由 发表于 2023年6月5日 10:21:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76403191.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定