我的输入在不应该运行时运行

编程入门 行业动态 更新时间:2024-10-10 03:23:07
本文介绍了我的输入在不应该运行时运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的问题是我试图用文本输出和所有内容制作一个更好,更压缩但仍然更高级的计算器.当我遇到问题时,它还处于非常非常早期的阶段.

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:

更多推荐

我的输入在不应该运行时运行

本文发布于:2023-11-23 13:22:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1621594.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时运

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!