我的问题是我试图用文本输出和所有内容制作一个更好,更压缩但仍然更高级的计算器.当我遇到问题时,它还处于非常非常早期的阶段.
my problem here is that I'm trying to make a better, more compressed but still advanced calculator with text outputs and everything. It's still in a very, VERY early stage as I came across a problem.
#!/usr/bin/python import math import time import random op=input('add/sub/mul/div/sqrt/n!/pow/ctof/hex') if op==('ctof'): choice=input('C to F or F to C. 1 or 2') if choice=='1': c=float(input('Input Celcius')) print(str(c)+' Degrees Celcius = '+str(c*1.8+32)+' Degrees Fahrenheit') if choice=='2': f=float(input('Input Fahrenheit')) print(str(f)+' Degrees Fahrenheit = '+str((f-32)/1.8)+' Degrees Celcius') else: print('Invalid operation') if op==('sqrt')or('n!'): x=float(input('Input X')) if op==('sqrt'): print('The square root of '+str(x)+' is: '+str(math.sqrt(x))) if op==('n!'): print('The factorial of '+str(x)+' is: '+str(math.factorial(x))) else: x=float(input('Input X')) y=float(input('Input Y')) if op==('add'): print(str(x)+' + '+str(y)+' = '+str(x+y))问题是平方根和阶乘条件中的x输入运行,而不是 else: 一.因此,当我使用add时,它说Y丢失了.我在做什么错了,为什么输入错误呢?
The thing is that the x inputin the square root and factorial condition runs instead of the else: one. So when I use add it says that Y is missing. What am I doing wrong and why does the wrong input start.
顺便说一句,对不起,代码混乱.如前所述,我希望它紧凑.另外,我认为亲自查看(如果可行)是一个很好的选择.
By the way, sorry for the messy code. As said I wanted it to be compact. Plus I think It's a beuty to look at personally(if it was working).
推荐答案所需的比较如下:
if op == 'sqrt' or op == 'n!':您的代码存在问题
if op == 'sqrt' or 'n!':是因为'n!'是恒定的真实值,所以它总是求值为true.它不会评估您认为的评估结果,它会执行以下操作:
is that it always evaluates to true because 'n!' is a constant truthy value. It does not evaluate what you think it evaluates, it does something like this:
if (op == 'sqrt') or 'n!':变成
if (op == 'sqrt') or True:更多推荐
我的输入在不应该运行时运行
发布评论