Creating a loop using Python (to simulate a “goto” like solution)











up vote
-1
down vote

favorite












Here is my approach to simulate a goto sequence. Is there a more eloquent way?



PS: the idea with storing the variable in a class variable was just for fun (because of the .(format()) accessing story.



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")









share|improve this question
























  • damnit i forgot the "break" after "..see you" xD
    – Günay Özcan
    Sep 8 at 22:45






  • 3




    What are you trying to do? What about this needs a goto?
    – jmucchiello
    Sep 8 at 22:49










  • actually its not about to become a "goto", since im new in python btw. in programming i remembered from c(i think) a thing called goto. But after a lot of research i discovered that goto is..crap(summary). so my curiosity is more like "is there a way to pull the code above in a more smart way down
    – Günay Özcan
    Sep 8 at 22:52






  • 1




    If you forgot something, you can edit your question to add it.
    – das-g
    Sep 8 at 23:04















up vote
-1
down vote

favorite












Here is my approach to simulate a goto sequence. Is there a more eloquent way?



PS: the idea with storing the variable in a class variable was just for fun (because of the .(format()) accessing story.



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")









share|improve this question
























  • damnit i forgot the "break" after "..see you" xD
    – Günay Özcan
    Sep 8 at 22:45






  • 3




    What are you trying to do? What about this needs a goto?
    – jmucchiello
    Sep 8 at 22:49










  • actually its not about to become a "goto", since im new in python btw. in programming i remembered from c(i think) a thing called goto. But after a lot of research i discovered that goto is..crap(summary). so my curiosity is more like "is there a way to pull the code above in a more smart way down
    – Günay Özcan
    Sep 8 at 22:52






  • 1




    If you forgot something, you can edit your question to add it.
    – das-g
    Sep 8 at 23:04













up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











Here is my approach to simulate a goto sequence. Is there a more eloquent way?



PS: the idea with storing the variable in a class variable was just for fun (because of the .(format()) accessing story.



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")









share|improve this question















Here is my approach to simulate a goto sequence. Is there a more eloquent way?



PS: the idea with storing the variable in a class variable was just for fun (because of the .(format()) accessing story.



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")






python json loops if-statement






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 23:31









halfer

14.2k757106




14.2k757106










asked Sep 8 at 22:42









Günay Özcan

11




11












  • damnit i forgot the "break" after "..see you" xD
    – Günay Özcan
    Sep 8 at 22:45






  • 3




    What are you trying to do? What about this needs a goto?
    – jmucchiello
    Sep 8 at 22:49










  • actually its not about to become a "goto", since im new in python btw. in programming i remembered from c(i think) a thing called goto. But after a lot of research i discovered that goto is..crap(summary). so my curiosity is more like "is there a way to pull the code above in a more smart way down
    – Günay Özcan
    Sep 8 at 22:52






  • 1




    If you forgot something, you can edit your question to add it.
    – das-g
    Sep 8 at 23:04


















  • damnit i forgot the "break" after "..see you" xD
    – Günay Özcan
    Sep 8 at 22:45






  • 3




    What are you trying to do? What about this needs a goto?
    – jmucchiello
    Sep 8 at 22:49










  • actually its not about to become a "goto", since im new in python btw. in programming i remembered from c(i think) a thing called goto. But after a lot of research i discovered that goto is..crap(summary). so my curiosity is more like "is there a way to pull the code above in a more smart way down
    – Günay Özcan
    Sep 8 at 22:52






  • 1




    If you forgot something, you can edit your question to add it.
    – das-g
    Sep 8 at 23:04
















damnit i forgot the "break" after "..see you" xD
– Günay Özcan
Sep 8 at 22:45




damnit i forgot the "break" after "..see you" xD
– Günay Özcan
Sep 8 at 22:45




3




3




What are you trying to do? What about this needs a goto?
– jmucchiello
Sep 8 at 22:49




What are you trying to do? What about this needs a goto?
– jmucchiello
Sep 8 at 22:49












actually its not about to become a "goto", since im new in python btw. in programming i remembered from c(i think) a thing called goto. But after a lot of research i discovered that goto is..crap(summary). so my curiosity is more like "is there a way to pull the code above in a more smart way down
– Günay Özcan
Sep 8 at 22:52




actually its not about to become a "goto", since im new in python btw. in programming i remembered from c(i think) a thing called goto. But after a lot of research i discovered that goto is..crap(summary). so my curiosity is more like "is there a way to pull the code above in a more smart way down
– Günay Özcan
Sep 8 at 22:52




1




1




If you forgot something, you can edit your question to add it.
– das-g
Sep 8 at 23:04




If you forgot something, you can edit your question to add it.
– das-g
Sep 8 at 23:04












1 Answer
1






active

oldest

votes

















up vote
0
down vote













Solution



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


This is how I would go about this, although there is no reason to have a class here for what you are trying to accomplish, but since you had it in there I'm assuming you were using it as practice, so I threw in a class here as well.



Edit



Ah you mentioned the class was for fun, not claiming I used class in the best fashion here but it's an improvement for the way you attempted, would definitely look over proper utilization of class.






share|improve this answer























  • thank you for the support man, your code looks neat :D
    – Günay Özcan
    Sep 9 at 11:01










  • Your welcome, but you are dong me a service as well! Helping others is a good way to learn, since you need to try to get a even better understanding to explain to someone else. Take time on here to look around and attempt to solve/learn from others questions, it helps.
    – vash_the_stampede
    Sep 9 at 12:10











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52239963%2fcreating-a-loop-using-python-to-simulate-a-goto-like-solution%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













Solution



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


This is how I would go about this, although there is no reason to have a class here for what you are trying to accomplish, but since you had it in there I'm assuming you were using it as practice, so I threw in a class here as well.



Edit



Ah you mentioned the class was for fun, not claiming I used class in the best fashion here but it's an improvement for the way you attempted, would definitely look over proper utilization of class.






share|improve this answer























  • thank you for the support man, your code looks neat :D
    – Günay Özcan
    Sep 9 at 11:01










  • Your welcome, but you are dong me a service as well! Helping others is a good way to learn, since you need to try to get a even better understanding to explain to someone else. Take time on here to look around and attempt to solve/learn from others questions, it helps.
    – vash_the_stampede
    Sep 9 at 12:10















up vote
0
down vote













Solution



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


This is how I would go about this, although there is no reason to have a class here for what you are trying to accomplish, but since you had it in there I'm assuming you were using it as practice, so I threw in a class here as well.



Edit



Ah you mentioned the class was for fun, not claiming I used class in the best fashion here but it's an improvement for the way you attempted, would definitely look over proper utilization of class.






share|improve this answer























  • thank you for the support man, your code looks neat :D
    – Günay Özcan
    Sep 9 at 11:01










  • Your welcome, but you are dong me a service as well! Helping others is a good way to learn, since you need to try to get a even better understanding to explain to someone else. Take time on here to look around and attempt to solve/learn from others questions, it helps.
    – vash_the_stampede
    Sep 9 at 12:10













up vote
0
down vote










up vote
0
down vote









Solution



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


This is how I would go about this, although there is no reason to have a class here for what you are trying to accomplish, but since you had it in there I'm assuming you were using it as practice, so I threw in a class here as well.



Edit



Ah you mentioned the class was for fun, not claiming I used class in the best fashion here but it's an improvement for the way you attempted, would definitely look over proper utilization of class.






share|improve this answer














Solution



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


This is how I would go about this, although there is no reason to have a class here for what you are trying to accomplish, but since you had it in there I'm assuming you were using it as practice, so I threw in a class here as well.



Edit



Ah you mentioned the class was for fun, not claiming I used class in the best fashion here but it's an improvement for the way you attempted, would definitely look over proper utilization of class.







share|improve this answer














share|improve this answer



share|improve this answer








edited Sep 8 at 23:12

























answered Sep 8 at 23:06









vash_the_stampede

3,7591319




3,7591319












  • thank you for the support man, your code looks neat :D
    – Günay Özcan
    Sep 9 at 11:01










  • Your welcome, but you are dong me a service as well! Helping others is a good way to learn, since you need to try to get a even better understanding to explain to someone else. Take time on here to look around and attempt to solve/learn from others questions, it helps.
    – vash_the_stampede
    Sep 9 at 12:10


















  • thank you for the support man, your code looks neat :D
    – Günay Özcan
    Sep 9 at 11:01










  • Your welcome, but you are dong me a service as well! Helping others is a good way to learn, since you need to try to get a even better understanding to explain to someone else. Take time on here to look around and attempt to solve/learn from others questions, it helps.
    – vash_the_stampede
    Sep 9 at 12:10
















thank you for the support man, your code looks neat :D
– Günay Özcan
Sep 9 at 11:01




thank you for the support man, your code looks neat :D
– Günay Özcan
Sep 9 at 11:01












Your welcome, but you are dong me a service as well! Helping others is a good way to learn, since you need to try to get a even better understanding to explain to someone else. Take time on here to look around and attempt to solve/learn from others questions, it helps.
– vash_the_stampede
Sep 9 at 12:10




Your welcome, but you are dong me a service as well! Helping others is a good way to learn, since you need to try to get a even better understanding to explain to someone else. Take time on here to look around and attempt to solve/learn from others questions, it helps.
– vash_the_stampede
Sep 9 at 12:10


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52239963%2fcreating-a-loop-using-python-to-simulate-a-goto-like-solution%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Full-time equivalent

さくらももこ

13 indicted, 8 arrested in Calif. drug cartel investigation