在Python 3.4中按回车退出While循环
Press Enter to exit While Loop in Python 3.4
我是 Python 的新手,过去几个月一直在自学。我正在使用的书教 Python 2.7,而我正在尝试学习 Python 3.4。我现在已经习惯了同时使用这两种方法,但是对于我的一生,我无法弄清楚如何使用回车键退出这个 while 循环。代码如下所示:
total = 0
count = 0
data = eval(input("Enter a number or press enter to quit: "))
while data != "":
count += 1
number = data
total += number
average = total / count
data = eval(input("Enter a number or press enter to quit: "))
print("The sum is", total, ". ", "The average is", average)
我不断收到此错误:
Traceback (most recent call last):
File "/Users/Tay/Documents/Count & Average.py", line 10, in <module>
data = eval(input("Enter a number or press enter to quit: "))
File "<string>", line 0
^
SyntaxError: unexpected EOF while parsing
我可以获得此代码的修改版本以在 2.7 中运行,但我想知道如何在 3.4 中执行此操作。我到处搜索,似乎找不到答案。
试试这个更正后的代码版本。你的逻辑是正确的,但你有一些错误。您不需要 eval
,在将其添加到总数时必须将数字转换为 integer
,最后您必须在打印出来之前在函数外部定义平均值。
total = 0
count = 0
average = 0
data = input("Enter a number or press enter to quit: ")
while data:
count += 1
number = data
total += int(number)
average = total / count
data = input("Enter a number or press enter to quit: ")
print("The sum is {0}. The average is {1}.".format(total, average))
例子:
Enter a number or press enter to quit: 5
Enter a number or press enter to quit: 4
Enter a number or press enter to quit: 3
Enter a number or press enter to quit: 2
Enter a number or press enter to quit:
The sum is 14. The average is 3.5.
Enter a number or press enter to quit:
The sum is 0. The average is 0.
将用户的输入保存为字符串,直到您检查其内容:
total = 0
count = 0
while 1:
data = input("Enter a number or press enter to quit: ")
try:
data = float(data)
except ValueError:
break
count += 1
total += data
average = total / count
print("The sum is " + total ". The average is " + average + ".")
我分离了一个 eval 函数 isNumber 我让它保持浮动小数,它看起来更干净一些。
def isNumber(value):
try:
float(value)
return True
except ValueError:
"error"
return False
total = 0
count = 0
data = input("Enter a number or press enter to quit: ")
while data and isNumber(data):
count += 1
number = float(data)
total += number
average = total / count #This sentences is more clean here (for me)
data = input("Enter a number or press enter to quit: ")
print("The sum is", total, ". ", "The average is", average)
我是 Python 的新手,过去几个月一直在自学。我正在使用的书教 Python 2.7,而我正在尝试学习 Python 3.4。我现在已经习惯了同时使用这两种方法,但是对于我的一生,我无法弄清楚如何使用回车键退出这个 while 循环。代码如下所示:
total = 0
count = 0
data = eval(input("Enter a number or press enter to quit: "))
while data != "":
count += 1
number = data
total += number
average = total / count
data = eval(input("Enter a number or press enter to quit: "))
print("The sum is", total, ". ", "The average is", average)
我不断收到此错误:
Traceback (most recent call last):
File "/Users/Tay/Documents/Count & Average.py", line 10, in <module>
data = eval(input("Enter a number or press enter to quit: "))
File "<string>", line 0
^
SyntaxError: unexpected EOF while parsing
我可以获得此代码的修改版本以在 2.7 中运行,但我想知道如何在 3.4 中执行此操作。我到处搜索,似乎找不到答案。
试试这个更正后的代码版本。你的逻辑是正确的,但你有一些错误。您不需要 eval
,在将其添加到总数时必须将数字转换为 integer
,最后您必须在打印出来之前在函数外部定义平均值。
total = 0
count = 0
average = 0
data = input("Enter a number or press enter to quit: ")
while data:
count += 1
number = data
total += int(number)
average = total / count
data = input("Enter a number or press enter to quit: ")
print("The sum is {0}. The average is {1}.".format(total, average))
例子:
Enter a number or press enter to quit: 5
Enter a number or press enter to quit: 4
Enter a number or press enter to quit: 3
Enter a number or press enter to quit: 2
Enter a number or press enter to quit:
The sum is 14. The average is 3.5.
Enter a number or press enter to quit:
The sum is 0. The average is 0.
将用户的输入保存为字符串,直到您检查其内容:
total = 0
count = 0
while 1:
data = input("Enter a number or press enter to quit: ")
try:
data = float(data)
except ValueError:
break
count += 1
total += data
average = total / count
print("The sum is " + total ". The average is " + average + ".")
我分离了一个 eval 函数 isNumber 我让它保持浮动小数,它看起来更干净一些。
def isNumber(value):
try:
float(value)
return True
except ValueError:
"error"
return False
total = 0
count = 0
data = input("Enter a number or press enter to quit: ")
while data and isNumber(data):
count += 1
number = float(data)
total += number
average = total / count #This sentences is more clean here (for me)
data = input("Enter a number or press enter to quit: ")
print("The sum is", total, ". ", "The average is", average)