微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

有人可以尝试将其做得更好一些,以便我可以看到我的错误或使其更有效率我是 python 的初学者

如何解决有人可以尝试将其做得更好一些,以便我可以看到我的错误或使其更有效率我是 python 的初学者

这是一个针对 covid-19 的测验,如果您有任何症状,它首先会询问您的姓名,然后询问您是否有任何症状,例如咳嗽或头痛。如果你说不,它会问更多的问题,如果你说是,它说你有 covid-19。它在最新的python中

name = input("your name is ") 

bad = input("hi " + name +  " do you have a cough ")

if bad == "no":
    bad2 = input("hello " + name + " do you have a sore throat ")
    if bad2 == "no":
        bad3 = input("hi " + name + " do you have a headache ")
        if bad3 == "no":
            bad4 = input("hi " + name + " do you have any pain ")
            if bad4 == "no":
                bad5 = input("hi " + name + " do you fell tired ")
                if bad5 == "no":
                    print("hi " + name + " you don't have covid")

if bad == "yes" or bad2 == "yes" or bad3 == "yes" or bad4 == "yes" or bad5 == "yes":
    print("sorry " + name + " you have covid-19 ..... and here a ticket of 1,000")

解决方法

使用更整洁和可读的方法完全相同。 “可读的代码是可维护的代码。”

name = input("Your name: ")
# array containing questions
q = [
    f"Hi,{name} do you have a cough?  ","Do you have a soar throat?  ","Do you have a headache?  ","Do you feel tired?  ",]
# user promt
for i in q:
    response = input(i)
    if response == "yes":
        print(f"-> Sorry {name},you have covid-19 ....." + 
            "and here's a ticket of 1,000.")
        break
else: 
    print("-> You don't have Covid.")

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。