-1

I am new to python, and I wanted to be able to restart the program from scratch with a code. I don't know what code I am supposed to use for this, but this is what my script looks like, and what I am aiming at. I saw this, but it didn't help since I don't know that much.

name = input("What is your name? ") print("Nice to meet you, " + name) js = input("What is your weight you would like to convert from lbs to kg? ") weight1 = int(js) * 0.453592 weight2 = str(weight1) print("Converted weight: " + weight2) restart = input("Would you like to restart the calculator (y/n)? ") if "y" in restart: # Code to restart program? if "n" in restart: print("Goodbye.") 

What can I insert in the comment to make this restart from the top?

3
  • 1
    Put your main code into a function, then run the function when the user chooses to. Commented Aug 10, 2021 at 20:51
  • "I saw this, but it didn't help since I don't know that much." isn't something we can help you with, because there isn't a way to understand what's preventing you from applying the advice. A general technique is explained there, and all we can really do here is explain it again. Commented Aug 10, 2021 at 20:59
  • I just wanted to put this out there to see if someone can write a better code and if I can just copy and paste the code into PyCharm (which is what I use) Commented Sep 28, 2021 at 1:50

1 Answer 1

2

Wrap your logic in a function. then call the function in your if statement.

def getWeight(name): print("Nice to meet you, " + name) js = input("What is your weight you would like to convert from lbs to kg? ") weight1 = int(js) * 0.453592 weight2 = str(weight1) return weight2 if y: getWeight(name) 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.