简体   繁体   English

我是 python 新手,我找不到为什么这个程序不起作用?

[英]I am new to python and i can't find why this program is not working?

def snt(a1, b1): 
     snt0 = a1+b1
     return snt0
def mnt(a1, b1): 
     mnt0 = a1*b1
     return mnt0
print("Hello to my console program")
print("This is a basic sum multiplication calculator ")
i=1
while (i>0):
    clicky=input("For sum click on 1 for multiplcation click on 2: ")
    if clicky==1:
        a=int(input("enter a"))
        b=int(input("enter b"))
        f=snt(a,b)   
        print(a ,"+", b ,"=",f)
        input("Click to go out")
        break
   elif clicky==2:
        a=int(input("enter a"))
        b=int(input("enter b"))
        f=mnt(a,b) before
        print(a ,"*", b ,"=", f)
        input("Click to go out")
        break
   else:
        i += 1

I can't understand why it's not working i know something is wrong with the while loop but i can't find it.我不明白为什么它不起作用我知道 while 循环有问题但我找不到它。 Note: I"ve just started learning python !注意:我刚刚开始学习 python !

input() returns a string, eg. input()返回一个字符串,例如。 "1" . "1" you need to cast it to an integer for your comparison to work.您需要将其转换为整数才能进行比较。

clicky = input(...)
clicky = int(clicky)

Here is the correct Answer:以下是正确答案:

def snt(a1, b1):
     snt0 = a1+b1
     return snt0
def mnt(a1, b1): 
     mnt0 = a1*b1
     return mnt0
print("Hello to my console program")
print("This is a basic sum multiplication calculator ")
i=1
while (i > 0):
   clicky = input("For sum click on 1 for multiplcation click on 2: ")
   clicky = int(clicky)
   if clicky == 1:
        a = int(input("enter a: "))
        b = int(input("enter b: "))
        f = snt(a,b)   
        print(a ,"+", b ,"=",f)
        input("Click to go out")
        break
   elif clicky == 2:
        a = int(input("enter a: "))
        b = int(input("enter b: "))
        f = mnt(a,b)
        print(a ,"+", b ,"=", f)
        input("Click to go out")
        break
   else:
        i += 1

The input on the variable clicky requires a cast, as you did correctly later for the variables a and b.变量 clicky 上的输入需要强制转换,正如您稍后对变量 a 和 b 所做的那样。

def snt(a1, b1):
    snt0 = a1 + b1
    return snt0

def mnt(a1, b1):
    mnt0 = a1 * b1
    return mnt0


print("Hello to my console program")
print("This is a basic sum multiplication calculator ")
i = 1
while (i > 0):
    clicky = int(input("For sum click on 1 for multiplcation click on 2: "))
    if clicky == 1:
        a = int(input("enter a: "))
        b = int(input("enter b: "))
        f = snt(a, b)
        print(a, "+", b, "=", f)
        input("Click to go out")
        break
    elif clicky == 2:
        a = int(input("enter a: "))
        b = int(input("enter b: "))
        f = mnt(a, b)
        print(a, "*", b, "=", f)
        input("Click to go out")
        break
    else:
        i += 1

Next time, when you open a ticket please try to make some effort to clarify what it is not working, what it is the error or the unexpected behavior you get.下次,当您打开故障单时,请尝试阐明它不工作的原因,是什么错误或您遇到的意外行为。

暂无
暂无

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

相关问题 我是 python 的新手,我想知道为什么这个 while 循环不起作用 - I am new to python and I am wondering why this while loop isn't working 我不明白为什么我不能让 python 中的 readline function 在这个程序中工作。 我究竟做错了什么? - I don't understand why i can't get readline function in python to work in this program. What am I doing wrong? 我正在尝试编写一个“机器人”服务员程序,但我似乎找不到我出错的地方 - I am trying to program a "robot" waiter program but I can't seem to find where I went wrong 我正在尝试通过 function 将任何 dict 转换为列表,但它不起作用并且无法找出原因 - I am trying to convert any dict to a list through a function, but it's not working and can't find out why 我使用 Python 制作了一个非常基本的脚本(我是新手),我不知道为什么它不工作,因为它应该工作 - I made a really basic script using Python (I am new at it) and i don't know why it is not working as it should work 我在我的套接字 python 程序中找不到错误 - I can't find the error in my socket python program 为什么我无法在当前工作目录中找到或定位 python 创建的“.txt”文件类型? - Why can't I find or locate a '.txt' filetype created by python on the current working directory? 我是 python 的新手,我正在努力学习它,我不明白为什么一个结果给了我一个不同的结果 - Im new to python and am trying to learn it, I can't figure out why one outcome is giving me a different one 为什么这段代码不起作用? 我是新手,我真的需要帮助 - Why isn't this code working? I am new and I really need assistance 我是 Python 的新手。 而且这个递归程序往往因为一些我无法弄清楚的奇怪原因而无法运行。 任何帮助将不胜感激 - I am new to Python. And this recursive program tends not to run for some strange reason I can't figure out. Any help would be grateful
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM