使用Python创建循环(模拟类似“goto”的解决方案)

使用Python创建循环(模拟类似“goto”的解决方案),python,json,loops,if-statement,Python,Json,Loops,If Statement,下面是我模拟goto序列的方法。有没有更雄辩的方法 PS:将变量存储在类变量中只是为了好玩(因为.(format())访问故事) n=0 while n==0: print("Whats your height?") height=input() print("Whats your age?") age=input() class loop: h=height a=age print("Your height i

下面是我模拟goto序列的方法。有没有更雄辩的方法

PS:将变量存储在类变量中只是为了好玩(因为.(format())访问故事)

n=0
while n==0:
    print("Whats your height?")
    height=input()
    print("Whats your age?")
    age=input()

    class loop:
        h=height
        a=age

    print("Your height is {answer.h}".format(answer=loop()))
    print("Would you like to continue?")
    answer=input()
    if answer=="yes":
        ++n
        print("alright ONE MORE TIME!")
    else:
        print("see you")

解决方案

class Person():
    def __init__(self,name, height, age):
        self.name = name
        self.height = height
        self.age = age

while True:
    name = input("What's your name?\n")
    height = input("What's your height?\n")
    age = input("What's your age?\n")

    user = Person(name, height, age)

    print(f"\nHello {(user.name).title()}, your height is {user.height} and you are" \
          f" {user.age} years old!")

    answer = input("\nWould you like to continue?('yes' or 'no')\n")

    if answer == 'yes':
        print("alright ONE MORE TIME!")
        continue
    else:
        print("See you!")
        break
这就是我要做的,虽然没有理由在这里为你想要完成的事情上一节课,但是既然你在那里有它,我假设你是在练习,所以我也在这里上了一节课

编辑


啊,你提到的
是为了好玩,并不是说我在这里用了
,但这是对你尝试的方式的改进,肯定会检查

的正确使用,该死,我忘了后面的“休息”。再见XD你想做什么?这需要一个goto吗?实际上它不会变成“goto”,因为我是python新手。顺便说一句,在编程过程中,我从c(我想)记得一个叫做goto的东西。但是经过大量研究,我发现goto是..垃圾(总结)。所以我的好奇心更像是"如果你忘了什么,你可以用你的问题来添加它。欢迎你,但你也为我提供了一项服务!帮助他人是一种很好的学习方式,因为你需要尝试更好地理解,以便向其他人解释。在这里花点时间四处看看,尝试解决问题e/向他人学习问题,这很有帮助。