python - 根据特定用户输入查找最早的和结尾的内容

标签 python

我刚刚开始使用 Python,正在尝试一个简单的练习。它是根据用户指定的输入结束程序,然后从输入的一组年龄中查找最大的年龄。我收到了用户输入,但遇到了最旧年龄方面的问题。我哪里出错了?我想我可能不是100%同意“无”这一点。下面是代码:

largest = None

while True:
    name = str(input("What is your name? enter Yvonne to end program."))
    if name.strip() != 'Yvonne':
        age = int(input("Please enter your age"))
    elif name.strip() == 'Yvonne':
        if largest is None or largest < age:
            largest = age
        print("Oldest age is: ",  largest)
        break

我得到的输出是错误的数字被选为最旧的:

enter image description here

最佳答案

您在错误的位置进行了最大测试:

largest = None

while True:
    name = str(input("What is your name? enter Yvonne to end program."))
    if name.strip() != 'Yvonne':
        age = int(input("Please enter your age"))
    
        if largest is None or largest < age:
            largest = age
    else:
        print("Oldest age is: ",  largest)
        break

关于python - 根据特定用户输入查找最早的和结尾的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65190009/

相关文章:

python - 类型错误:启动 Spyder 5.2.2 时出现预期字符串或类似字节的对象

python - Python 中的编译警告

python - QueryDict 不包含隐藏的表单字段,给出 MultiValueDictKeyError

python - 输入时间限制/倒计时

python - AttributeError : module 'torch' has no attribute '_six' . Pytorch 中的 Bert 模型

python - 如何在 pymongo 中检查该项目不在列表字段中?

python - 中断后重新启动 for 循环

python - 字典排序不正确键 :value

python - 重新启动后Pyserial的串行连接问题

Python pivot_table - 添加差异列