使用Min&Max python的While语句

2024-06-17 15:34:42 发布

您现在位置:Python中文网/ 问答频道 /正文

while True:
    reply = raw_input("Enter text, (type [stop] to quit): ")
    print reply.lower()
    if reply == 'stop':
        break
    x = min(reply)
    y = max(reply)
    print("Min is " + x)
    print("Max is " + y)

我试着做一个包含while语句的语句,它请求一系列输入语句,并获取所有输入的语句,然后找到所有输入数字的最小值和最大值。有人有办法吗?我想解决这个问题已经有一段时间了,但没有任何运气。谢谢大家!在


Tags: totexttrueinputrawistype语句
3条回答

这是另一种方法。在

while True:
    reply = raw_input("Enter numbers separated by commas. (type [stop] to quit): ")
    user_input = reply.split(',')
    if reply == 'stop':
        break
    x = map(float, user_input)
    y = map(float, user_input)
    values = (x, y)
    print("Min is " + str(min(x)))
    print("Max is " + str(max(y)))

输入:

5, 10, 5000

输出:

^{pr2}$

缩进在Python中很重要。至于minmax,可以有两个变量来跟踪这些数字并继续请求数字,直到停止条件为止。在

min = max = userInput = raw_input()
while userInput != "stop":
    if int(userInput) < int(min):
        min = int(userInput)
    elif int(userInput) > int(max):
        max = int(userInput)
    userInput = raw_input()
    print "Min is "+str(min)
    print "Max is "+str(max)

这里,第一个输入作为minmax值。注意,如果用户为第一个值输入stop,那么min和{}也将是{}。如果你能为用户输入澄清更多的限制,那会更好。在

你的思路是正确的。您没有使用minmax作为变量名,这也很好。如果您使用python3,请在下面的代码中将input关键字替换为raw_nput。在

希望成功了!:)

minn=5000;
maxx=0;
while True:
    reply = input("Enter text, (type [stop] to quit): ")

    if int(reply) < int(minn):
        minn = int(reply)

    if int(reply) > int(maxx):
        maxx = int(reply)

    if reply == 'stop':
        break
    print("Min is " + str(minn))
    print("Max is " + str(maxx))  

相关问题 更多 >