0

I just learn my first python and try to make a continuous loop that has a user input condition.

#Make the calculating func
def data_cal():
    pennies = int(input("What's your pennies?"))
    dollars = pennies // 100
    cents = pennies % 100
    print("You have $", dollars, "and", cents, "cents")
data_cal()
#User input for answer
repeat = input("Do you want to try again?")
answer = ['yes','YES','Yes','y','Y']
#Loop for answer
while repeat in answer
    data_cal()
else: print("Bye then")

I was thinking if I can recall repeat after I called data_cal() and, or another if statement

…..
while repeat in answer
    data_cal()
    if repeat in answer:
      repeat (#this step I tried to recall repeat, is this possible?, any other way to get around this?)
    else: break
print ("Bye then")

Please bear with me, I am very new to programming language and might not express myself very clear. The idea is to call the data_cal() for the first time then, ask for user input -("Do you want to try again?") - if the input is yes then recall data_cal() and then RE ASK ("Do you want to try again?") and repeat the cycle, if the input is no then print("Bye") Thank you very much!

2
  • you want to continue the program until user enters 'n' or 'No'? Commented Oct 9, 2019 at 8:00
  • I believe you should be able to just copy the repeat = input() line into the loop, after the function call Commented Oct 9, 2019 at 8:00

4 Answers 4

1

You have to ask the user inside of the while loop, if he wants to try again (whatever is done in data_cal()). Otherwise the given answer can never change.

answer = ['yes','YES','Yes','y','Y']
repeat = 'yes'

#Loop for answer
while repeat in answer
    data_cal()
    repeat = input("Do you want to try again?")
else: print("Bye then")
Sign up to request clarification or add additional context in comments.

2 Comments

This doesn't make sense when you didn't define repeat before you call it in while loop, does it? .. correct me if I'm wrong. Ty
You are absolutely right. You have to give repeat a sensible default value, so that you enter the loop. I'll update the answer.
0
#Make the calculating func
repeat = ""
def data_cal():
    pennies = int(input("What's your pennies?"))
    dollars = pennies // 100
    cents = pennies % 100
    print("You have $", dollars, "and", cents, "cents")
    repeat = input("Do you want to try again?")
    return repeat

repeat = data_cal()
#User input for answer
answer = ['yes','YES','Yes','y','Y']
#Loop for answer

while repeat in answer:
    repeat = data_cal()
else:
    print("Bye then")

From the code format you have written, just move the repeat assignment line to data_cal(), and return the value so that you can use that in the while loop.

2 Comments

And dont forget the colon(:) for the while statement :-)
No problem :-) Please do upvote the answer if you found it useful.
0

You can use continue and breake to control your loop,then you can write like this

#Make the calculating func
def data_cal():
    pennies = int(input("What's your pennies?"))
    dollars = pennies // 100
    cents = pennies % 100
    print("You have $", dollars, "and", cents, "cents")

answer = ['yes','YES','Yes','y','Y']
#Loop for answer
while True:
    data_cal()
    repeat = input("Do you want to try again?")
    if repeat in answer:
        data_cal()
        continue
    else:
        print("Bye then")
        break

Comments

0

You can use the while loop inside the function.

#Make the calculating func
    repeat = "yes"
    answer = ['yes','YES','Yes','y','Y']

    def data_cal():
        global repeat
        while repeat in answer:
            pennies = int(input("What's your pennies?"))
            dollars = pennies // 100
            cents = pennies % 100
            print("You have $", dollars, "and", cents, "cents")
            repeat = input("Do you want to try again?")

    data_cal()
    print("Bye then")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.