【问题标题】:Python - how to go back to specific line [duplicate]Python - 如何返回特定行[重复]
【发布时间】:2020-05-10 05:41:19
【问题描述】:

我刚开始学习python一周。

我正在尝试做一个测试:


"Beginning of the Program"
print("Hi Sir and welcome to my calculator")
print("Please select from the below menu your math operation")

MenuOption = input("+ for Add, - for Subtract, * for Multiply, / for Division: ") 

if (MenuOption != "+" and MenuOption != "-" and MenuOption != "*" and MenuOption != "/"):
    print("You have typed wrong character, please try again")
    **# my problem is here, I want it to loop back to MenuOption  line**

else:
    print("Thanks for the correct selection")



FirstNumber = int(input("First Number: "))
SecondNumber = int(input("Second Number: "))

if (MenuOption == "+"):
    print(FirstNumber + SecondNumber)

elif (MenuOption == "-"):
    print(FirstNumber - SecondNumber)              

elif (MenuOption == "*"):
    print(FirstNumber * SecondNumber)

elif (MenuOption == "/"):
    print(FirstNumber / SecondNumber)

当我不按任何数学符号(+、-、*、/)时,我希望程序开始恢复 MenuOption

【问题讨论】:

  • 另见:1234, 5 都是相关的。
  • 感谢您的工作

标签: python python-3.x


【解决方案1】:

您需要学习循环。如果您执行while 的操作,循环表达式将变为false,它将正常运行。当将选择其中一个符号+ - / * 时,将某个变量设置为false,否则,flag 变量应为true

注意

那是因为python 也不支持gotolabel

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-24
    • 2019-04-26
    • 2016-09-19
    • 2018-12-14
    • 2017-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多