简体   繁体   English

我怎么不打印两次

[英]How do i not print it twice

I'm new to python and experimenting with functions.我是 python 的新手,正在尝试功能。 Here's the sample code that I'm working wtih这是我正在使用的示例代码

def menu():
    print(" 1. Divide")

def test1(x,y):
    if y == 0:
        return "The result is undefined"
    else:
        return x/y

num1=int(input("First: "))
num2=int(input("Second: "))

menu()
answer=int(input("Choose: "))

while answer != 0:
    if answer == 1:
        print()
        print(" The result is", test1(num1,num2))
    print()
    menu()
    answer=int(input("Choose: "))

When I run the program and input ay value of 0, the result prints twice.当我运行程序并输入 y 值 0 时,结果打印两次。 How do I make it print once only then return to menu?如何让它只打印一次然后返回菜单? Thank you谢谢

This is not a bug in your code, you can simply Change your zero condition to this:这不是代码中的错误,您只需将零条件更改为:

if y == 0:
    return "undefined"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM