代码有什么问题?
What is wrong in the code?
我是编程和堆栈溢出的新手。我选择 python 作为我的第一语言。今天,当我写一些代码来刷新和提高我的技能时,我写了一个小程序。但是完全错误。
这是程序
a = [1 , 2, 3]
def list_append():
numbers = int(raw_input("Enter the number please"))
a.append(numbers)
print a
def average(list):
for marks in list:
print marks
total = float(sum(list))
total = total / len(list)
print ("Your total average is : %d" %total )
def loop():
add_numbers = raw_input("Do you want to add another number")
if add_numbers == ("y"):
return list_append()
else:
return average()
while True:
loop()
print average(a)
基本上这个程序的功能是要求用户输入号码。然后附加到列表,然后显示平均值,这是一个简单的方法。
但我希望程序在第一次输入后停止并询问用户是否要再输入一次?
不明白问题出在哪里。 ** 我不是在寻求直接的解决方案。我宁愿要一个解释而不是解决方案本身。**
a = []
def average(list):
total = float(sum(list))
total = total / len(list)
print ("Your total average is : %d" %total )
while True:
numbers = raw_input("Enter the number please or 'q' to quit : ")
if numbers == "q":
average(a)
break
else:
a.append(int(numbers))
希望对您有所帮助
您的代码中缺少以下内容:
- 需要打破任何循环,你的 while 循环进入无限循环。
while True:
loop()
2. 处理类型转换期间的异常。
numbers = int(raw_input("Enter the number please"))
- 在循环函数中创建用户输入数字列表并传递给list_append 函数以添加数字。
还 return 从循环函数将参数传递给平均函数。
代码:
def list_append(numbers):
while 1:
try:
no = int(raw_input("Enter the number please:"))
numbers.append(no)
break
except ValueError:
print "Enter only number."
return list(numbers)
def average(number_list):
avg = float(sum(number_list))/ len(number_list)
return avg
def loop():
numbers = []
while 1:
add_numbers = raw_input("you want to add number in list(Y):")
if add_numbers.lower()== ("y"):
numbers = list_append(numbers)
else:
return list(numbers)
numbers = loop()
avg = average(numbers)
print "User enter numbers:", numbers
print "average value of all enter numbers:", avg
输出:
vivek@vivek:~/Desktop/Whosebug$ python 17.py
you want to add number in list(Y):y
Enter the number please:10
you want to add number in list(Y):y
Enter the number please:e
Enter only number.
Enter the number please:20
you want to add number in list(Y):Y
Enter the number please:30
you want to add number in list(Y):n
User enter numbers: [10, 20, 30]
average value of all enters numbers: 20.0
vivek@vivek:~/Desktop/Whosebug$
不要使用已经由 python 定义的变量名
例如list
>>> list
<type 'list'>
>>> list([1,2,3])
[1, 2, 3]
>>> list = [2]
>>> list([1,2,3])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'list' object is not callable
>>>
我是编程和堆栈溢出的新手。我选择 python 作为我的第一语言。今天,当我写一些代码来刷新和提高我的技能时,我写了一个小程序。但是完全错误。 这是程序
a = [1 , 2, 3]
def list_append():
numbers = int(raw_input("Enter the number please"))
a.append(numbers)
print a
def average(list):
for marks in list:
print marks
total = float(sum(list))
total = total / len(list)
print ("Your total average is : %d" %total )
def loop():
add_numbers = raw_input("Do you want to add another number")
if add_numbers == ("y"):
return list_append()
else:
return average()
while True:
loop()
print average(a)
基本上这个程序的功能是要求用户输入号码。然后附加到列表,然后显示平均值,这是一个简单的方法。
但我希望程序在第一次输入后停止并询问用户是否要再输入一次?
不明白问题出在哪里。 ** 我不是在寻求直接的解决方案。我宁愿要一个解释而不是解决方案本身。**
a = []
def average(list):
total = float(sum(list))
total = total / len(list)
print ("Your total average is : %d" %total )
while True:
numbers = raw_input("Enter the number please or 'q' to quit : ")
if numbers == "q":
average(a)
break
else:
a.append(int(numbers))
希望对您有所帮助
您的代码中缺少以下内容:
- 需要打破任何循环,你的 while 循环进入无限循环。
while True:
loop()
2. 处理类型转换期间的异常。
numbers = int(raw_input("Enter the number please"))
- 在循环函数中创建用户输入数字列表并传递给list_append 函数以添加数字。 还 return 从循环函数将参数传递给平均函数。
代码:
def list_append(numbers):
while 1:
try:
no = int(raw_input("Enter the number please:"))
numbers.append(no)
break
except ValueError:
print "Enter only number."
return list(numbers)
def average(number_list):
avg = float(sum(number_list))/ len(number_list)
return avg
def loop():
numbers = []
while 1:
add_numbers = raw_input("you want to add number in list(Y):")
if add_numbers.lower()== ("y"):
numbers = list_append(numbers)
else:
return list(numbers)
numbers = loop()
avg = average(numbers)
print "User enter numbers:", numbers
print "average value of all enter numbers:", avg
输出:
vivek@vivek:~/Desktop/Whosebug$ python 17.py
you want to add number in list(Y):y
Enter the number please:10
you want to add number in list(Y):y
Enter the number please:e
Enter only number.
Enter the number please:20
you want to add number in list(Y):Y
Enter the number please:30
you want to add number in list(Y):n
User enter numbers: [10, 20, 30]
average value of all enters numbers: 20.0
vivek@vivek:~/Desktop/Whosebug$
不要使用已经由 python 定义的变量名
例如list
>>> list
<type 'list'>
>>> list([1,2,3])
[1, 2, 3]
>>> list = [2]
>>> list([1,2,3])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'list' object is not callable
>>>