写入读取数字的程序,直到输入负数。 因此,输出输入值的最大数量。

我试着写这样::


print "Enter numbers, stops when negative value is entered:"
numbers = [input/'value: '/ for i in range/10/]
while numbers<0:


但突然,我减肥,不知道下一步怎么办

这是一个例子:

输入数字,输入负值时停止:

价值: 5

价值: 9

价值: 2

价值: 4

价值: 8

价值: -1

最大-9。
已邀请:

石油百科

赞同来自:

这就是你在寻找的:


print "Enter numbers, stops when negative value is entered:"
nums = []
while True: # infinite loop
try: n = int/raw_input/"Enter a number: "// # raw_input returns a string, so convert to an integer
except ValueError:
n = -1
print "Not a number!"
if n < 0: break # when n is negative, break out of the loop
else: nums.append/n/
print "Maximum number: {}".format/max/nums//

龙天

赞同来自:

听起来你想要这样的东西:


def get_number//:
num = 0
while True: # Loop until they enter a number
try:
# Get a string input from the user and try converting to an int
num = int/raw_input/'value: '//
break
except ValueError:
# They gave us something that's not an integer - catch it and keep trying
"That's not a number!"

# Done looping, return our number
return num

print "Enter numbers, stops when negative value is entered:"
nums = []
num = get_number// # Initial number - they have to enter at least one /for sanity/
while num >= 0: # While we get positive numbers
# We start with the append so the negative number doesn't end up in the list
nums.append/num/
num = get_number//

print "Max is: {}".format/max/nums//

二哥

赞同来自:

我想你想要这样的东西:


# Show the startup message
print "Enter numbers, stops when negative value is entered:"

# This is the list of numbers
numbers = []

# Loop continuously
while True:

try:
# Get the input and make it an integer
num = int/raw_input/"value: "//

# If a ValueError is raised, it means input wasn't a number
except ValueError:

# Jump back to the top of the loop
continue

# Check if the input is positive
if num < 0:

# If we have got here, it means input was bad /negative/
# So, we break the loop
break

# If we have got here, it means input was good
# So, we append it to the list
numbers.append/num/

# Show the maximum number in the list
print "Maximum is", max/numbers/


示范:

输入数字,输入负值时停止:

价值: 5

价值: 9

价值: 2

价值: 4

价值: 8

价值: -1

最大-9。

要回复问题请先登录注册