python - 尝试为用户提供通过调用函数来重新启动程序的机会,但它不起作用

标签 python python-3.x restart

我正在开发一个小型 python 计算器,一旦它运行,我想询问用户是否要重新启动,然后根据他们的响应重新启动程序。

我有这个:

def begin():
    print("WELCOME TO THE PYTHON CALCULATOR\n\nYOUR SELECTIONS:\nBasic (+, -, *, /)\nAdvanced (power, square root)\nBMI\nMortgage\nTrip\n")
    selection = input("What type of Calculator would you like to use? (NB: Case sensitive): ")

    if selection == "Basic":
        basic_input()
    if selection == "Advanced":
        adv_input()
    if selection == "BMI":
        imp_or_met = input("Do you prefer to use metric or imperial units? (Metric/Imperial): ")
        if imp_or_met == "Metric":
            bmi_met()
        elif imp_or_met == "Imperial":
            bmi_imp()
    if selection == "Mortgage":
        mort_calc()
    if selection == "Trip":
        trip_calc()

begin()
#restart calculator
restart = print(input("Would you like to use the calculator again?\n1: Yes, 2: No\n"))
if restart == "1":
    begin()
else:
    print("Thank you for using the calculator!")

但这是输出(来自询问用户是否要重新开始的问题):

Would you like to use the calculator again?
1: Yes, 2: No
1
Thank you for using the calculator!

我对编码非常陌生,所以我很感激这似乎是一个非常微不足道的问题:)...但我很感谢这里的任何帮助!

非常感谢!

最佳答案

restart = input("Would you like to use the calculator again?\n1: Yes, 2: No\n")

if restart == "1":
    begin()
else:
    print("Thank you for using the calculator!")

问题是您将 print(input(...)) 分配给 restart,即 NoneType

关于python - 尝试为用户提供通过调用函数来重新启动程序的机会,但它不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63040973/

相关文章:

python - Execfile 与导入一起使用时运行文件两次

python - 使用按键重新启动 While 循环

multithreading - 编写一个 bash 脚本来重启一个守护进程

python - 如何解决 Django 中的模块名称冲突?

python-3.x - 创建一个弹出菜单来调用自定义导出器(Blender 2.6 API)?

python - pycharm错误:cannot perform refactoring using selected element(s)

iphone - iphone 重启后 iOS 应用程序需要登录

python - 统计按 ID 分组的列表中的更改

Python3 在网络摄像头 fps 处处理和显示网络摄像头流

python 绝地: how to retrieve methods of instances?