[英]Python ask for user input without a newline?
I just started python a few days ago and have been working on a calculator (not extremely basic, but also not advanced). 我几天前才刚开始使用python,并且一直在使用计算器(不是很基础,但也不是高级)。 The problem doesn't prevent code from running or anything, it is just a visual thing. 该问题不会阻止代码运行或执行任何操作,而只是视觉上的事情。 Output in the console looks like this (stuff in parenthesis is explaining what is happening and is not actually part of the output): 控制台中的输出看起来像这样(括号中的内容正在解释发生了什么,而实际上并不是输出的一部分):
4 (user prompted for first number, press enter afterwards)
+ (user prompted for an operator, press enter afterwards
5 (user prompted for second number, press enter afterwards)
9.00000 (answer is printed)
Process finished with exit code 0
Basically what I want it to look like is this when I'm entering it into the console: 基本上,当我将其输入控制台时,我希望它看起来像这样:
4+5
9.00000
I don't want it to start a newline after I enter a number or operator or whatever, it looks more like an actual calculator when it prints along one line. 输入数字或运算符等后,我不希望它开始换行符,当它沿一行打印时,它看起来更像是一个实际的计算器。 Is this possible to do and if so how? 这可能吗?如果可以,怎么办? Btw I know end=""
works with print
but not with input
since it doesn't accept arguments. 顺便说一句,我知道end=""
可用于print
但不能用于input
因为它不接受参数。 Also I know the whole calculator thing is kind of redundant considering you can make calculations really easily in the python IDLE but I thought it was a good way for me to learn. 我也知道整个计算器是多余的,考虑到您可以在python IDLE中真正轻松地进行计算,但是我认为这是我学习的好方法。 Here is the entire code if you need it: 如果需要,这里是完整的代码:
import math
while True:
try:
firstNumber = float(input())
break
except ValueError:
print("Please enter a number... ", end="")
while True:
operators = ['+', '-', '*', '/', '!', '^']
userOperator = str(input())
if userOperator in operators:
break
else:
print("Enter a valid operator... ", end="")
if userOperator == operators[4]:
answer = math.factorial(firstNumber)
print(answer)
pause = input()
raise SystemExit
while True:
try:
secondNumber = float(input())
break
except ValueError:
print("Please enter a number... ", end="")
if userOperator == operators[0]:
answer = firstNumber + secondNumber
print('%.5f' % round(answer, 5))
elif userOperator == operators[1]:
answer = firstNumber - secondNumber
print('%.5f' % round(answer, 5))
elif userOperator == operators[2]:
answer = firstNumber * secondNumber
print('%.5f' % round(answer, 5))
elif userOperator == operators[3]:
answer = firstNumber / secondNumber
print('%.5f' % round(answer, 5))
elif userOperator == operators[5]:
answer = firstNumber ** secondNumber
print('%.5f' % round(answer, 5))
pause = input()
raise SystemExit
Your problem is that you're asking for input()
without specifying what you want. 您的问题是,您在没有指定所需内容的情况下要求input()
。 So if you take a look at the first one: firstNumber = float(input())
It's executing properly, but you hit enter
it gives an error which is only then you're specifying what you want. 因此,如果您看一下第一个: firstNumber = float(input())
它正在正确执行,但是按enter
会给出一个错误,该错误只有在您指定所需内容的情况下。
Try replacing with these: 尝试替换为:
...
try
firstNumber = float(input("Please enter a number... "))
...
userOperator = str(input("Enter a valid operator... "))
...
secondNumber = float(input("Please enter a number... "))
Is that what you're looking for? 那是您要找的东西吗?
Using my method I suggested: 使用我的方法,我建议:
Please enter a number... 5
Enter a valid operator... +
Please enter a number... 6
11.00000
Using your method: 使用您的方法:
Please enter a number... 5
Enter a valid operator... +
Please enter a number... 6
11.00000
Extra newlines which is what I'm assuming you're referring to. 我假设您要指的是多余的换行符。
That's a nice exercise , and as I wrote in the comments, I would ignore whitespaces, take the expression as a whole from the user and then parse it and calculate the result. 这是一个很好的练习 ,正如我在评论中所写,我将忽略空格,从用户那里获取整个表达式,然后对其进行解析并计算结果。 Here's a small demo: 这是一个小演示:
def is_number(s):
try:
float(s)
return True
except ValueError:
return False
def calc(expr):
if is_number(expr):
return float(expr)
arr = expr.split('+')
if len(arr) > 1:
return sum(map(calc, arr))
arr = expr.split('-')
if len(arr) > 1:
return reduce(lambda x,y: x-y, map(calc, arr))
arr = expr.split('*')
if len(arr) > 1:
return reduce(lambda x,y: x*y, map(calc, arr), 1)
arr = expr.split('/')
if len(arr) > 1:
return reduce(lambda x,y: x/y, map(calc, arr))
print calc("3+4-2 *2/ 2") # 5
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.