Trouble with Loops in lab assignment - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Trouble with Loops in lab assignment (/thread-20101.html) |
Trouble with Loops in lab assignment - jonstryder - Jul-28-2019 Hi, so this is my first time in this forum. I'm new to python coding, and so far the majority of my coding experience has been in regards to web design/development so python has been a massive departure from everything that I've learned so far...well almost. I see similarities with javascript, but I'm not any good with that either. To the point of the post! We're into loops now, and I've only just managed to figure out modules. This assignment has the following requirements:
I'm recycling the code from the previous lab to try and incorporate this new type of coding into it. To help, here is the coding from my last lab (including pseudocode): __author__ = 'Sean Ropp' # The program is based off of a card game by RoosterTeeth called "A Million Dollars...But." # You choose from options A, B, or C. Any other option will ask that you refresh the program. # Input List: answer # Output List: Answer # Module String Question() # Display "We are going to play a game." # Display "You get a million dollars...but," # Display " A: Every time you spend a dollar, someone magically appears and licks your face." # Display " B: Once a week you you have to deal with a ninja stalking you." # Display " C: Anytime you use a bathroom, or restroom, a band of paparazzi appear and take pictures of you." # Display "" # End Module def question(): print("We are going to play a game.") print("You get a million dollars...but,") print(" A: Every time you spend a dollar, someone magically appears and licks your face.") print(" B: Once a week you you have to deal with a ninja stalking you.") print(" C: Anytime you use a bathroom, or restroom, a band of paparazzi appear and take pictures of you.") print("") # Function String answer_choice(): # Declare String answer # # Display "Enter your Choice: " # Input answer # # If answer == 'A' or answer == 'a': # Display "You have chosen A." # Else If answer == 'B' or answer == 'b': # Display "You have chosen B." # Else If answer == 'C' or answer == 'c': # Display "You have chosen C.") # Else: # Display "Refresh and try again." # Return answer # End Function def answer_choice(): answer = input("Enter your Choice: ") if answer == 'A' or answer == 'a': print("You have chosen A.") elif answer == 'B' or answer == 'b': print("You have chosen B.") elif answer == 'C' or answer == 'c': print("You have chosen C.") else: print("Refresh and try again.") return answer # Function String gotten_into(answer) # Display "" # If answer == 'A' or answer == 'a': # Display "This person appears anywhere." # Display """You're standing in a grocery store and buying a soda and some chips.""" # Display "This guy just appears and licks your face for every dollar you just spent." # Display "Just bought a house? Guess who congratulates you for every dollar spent?" # Else If answer == 'B' or answer == 'b': # Display "The ninja appears at some time, once per week, for the next 24hrs." # Display "They never attack you, but they are always there watching you, making threatening gestures." # Display "And they will set traps, use poisons, and just stealthfully make your life difficult for one day." # Display "Their tricks are never harmful, and you can get out of them, or recover from them in a minute." # Else If answer == 'C' or answer == 'c': # Display "They appear and crowd into the bathroom, pushing anyone else around all to catch a picture" # Display "of you doing your business." # Display """They also call out really personal questions like, "are you getting enough fiber,""" # Display """is it the hershey squirts," and other such questions.""" # Display "You then have to push your way through them to get out, but once your out of the" # Display "bathroom they vanish. If you check the tabloids at all, your in them the next day." # Else: # Display "Seriously, make a choice of A, B, or C. No other choice will work." # Return answer def gotten_into(answer): print("") if answer == 'A' or answer == 'a': print("This person appears anywhere.") print("""You're standing in a grocery store and buying a soda and some chips.""") print("This guy just appears and licks your face for every dollar you just spent.") print("Just bought a house? Guess who congratulates you for every dollar spent?") elif answer == 'B' or answer == 'b': print("The ninja appears at some time, once per week, for the next 24hrs.") print("They never attack you, but they are always there watching you, making threatening gestures.") print("And they will set traps, use poisons, and just stealthfully make your life difficult for one day.") print("Their tricks are never harmful, and you can get out of them, or recover from them in a minute.") elif answer == 'C' or answer == 'c': print("They appear and crowd into the bathroom, pushing anyone else around all to catch a picture") print("of you doing your business.") print("""They also call out really personal questions like, "are you getting enough fiber,""") print("""is it the hershey squirts," and other such questions.""") print("You then have to push your way through them to get out, but once your out of the") print("bathroom they vanish. If you check the tabloids at all, your in them the next day.") else: print("Seriously, make a choice of A, B, or C. No other choice will work.") return answer # Module display_all() # Call question() # # Declare answer # # Set answer = answer_choice() # Set answer = gotten_into(answer) # End Module def displaying_all(): question() answer = answer_choice() answer = gotten_into(answer) displaying_all()To meet the requirements for the class, the too loops that I'm thinking of is to stop and check if the answer you want is right. "Are you sure you want this? Yes/No" Then after marking yes or now, it displays the last module that shows the outcome of your first choice. BUT, after it shows this, I want to make a loop that will allow you to start the little game over again and try out another answer option without having to rerun the whole program. Now, what I've started to do is placing my new loop module after this module: def answer_choice():What I've written so far is (I'll do the pseudo later, for some reason it's easier for me to do that last, not first): def are_you_sure(answer): yes_or_no = input("Yes / No") for answer_choice = "A" or "a" or "B" or "b" or "C" or "c" print("Are you sure you want " answer "?" yes_or_no) if yes_or_no == 'Y' or yes_or_no'y':This is where I'm at and I'm certain that this isn't the way to go with this, but the how to actually go about this in a simple manner is escaping me. Then of course there is the issue of looping back to the start of it all and how that can be done. I will mention that our class book is trash on showing code in action (it mainly displays the pseudocode and not the actual functioning code that makes it all work). It talks at you and doesn't really show you (if that helps in explaining things). And asking the instructor isn't helpful since it can take a day or two to hear back from them. Please, any instruction would be greatly appreciated. RE: Trouble with Loops in lab assignment - ichabod801 - Jul-28-2019 We happen to have a tutorial on validating user input that uses loops. RE: Trouble with Loops in lab assignment - jonstryder - Jul-28-2019 I've glanced through it, but it doesn't seem to cover putting a loop in a module or the second half of what I'm trying to do. While I see this as helpful, it's also not helpful. Coming up with validation isn't the problem so much as how should I do the loops for what I'm wanting to do. That's where I'm confused. I guess the with the second module I'm basically validating a validation. "You have chosen option a." "Are you sure? Y/N" I know that if the person chooses Y, they should be able to move on, but if they choose N the they start over with their options. Then at the end, the 3rd module, you get the answer to your choice. But then I also need to allow you to essentially go back to the beginning. I'm not sure if this helps in clearing up what I'm looking to try and do. RE: Trouble with Loops in lab assignment - ThomasL - Jul-28-2019 Have a look at this: def question(): print("We are going to play a game.") print("You get a million dollars...but,") print(" A: Every time you spend a dollar, someone magically appears and licks your face.") print(" B: Once a week you you have to deal with a ninja stalking you.") print(" C: Anytime you use a bathroom, or restroom, a band of paparazzi appear and take pictures of you.") print(" X: You do not want the money, live a poor life and quit.") print() def are_you_sure(answer): while True: print("Are you sure you want {}? ".format(answer), end='') yes_or_no = input("(Y)es / (N)o") if yes_or_no in ['Yes', 'yes', 'Y', 'y']: return 'Y' elif yes_or_no in ['No', 'no', 'N', 'n']: return 'N' def answer_choice(): while True: answer = input("Enter your Choice: ") if answer in ['A', 'B', 'C', 'X', 'a', 'b', 'c', 'x']: answer = answer.upper() print("You have chosen {}. ".format(answer), end='') if are_you_sure(answer) == 'Y': return answer else: print("Seriously, make a choice of A, B, C or X. No other choice will work.") def gotten_into(answer): print() if answer == 'A': print("This person appears anywhere.") print("""You're standing in a grocery store and buying a soda and some chips.""") print("This guy just appears and licks your face for every dollar you just spent.") print("Just bought a house? Guess who congratulates you for every dollar spent?") print() elif answer.lower() == 'B': print("The ninja appears at some time, once per week, for the next 24hrs.") print("They never attack you, but they are always there watching you, making threatening gestures.") print("And they will set traps, use poisons, and just stealthfully make your life difficult for one day.") print("Their tricks are never harmful, and you can get out of them, or recover from them in a minute.") print() elif answer.lower() == 'C': print("They appear and crowd into the bathroom, pushing anyone else around all to catch a picture") print("of you doing your business.") print("""They also call out really personal questions like, "are you getting enough fiber,""") print("""is it the hershey squirts," and other such questions.""") print("You then have to push your way through them to get out, but once your out of the") print("bathroom they vanish. If you check the tabloids at all, your in them the next day.") print() elif answer.lower() == 'X': print("Oh it seems you are a clever person. See you next time. Bye.") return answer def displaying_all(): while True: question() answer = answer_choice() answer = gotten_into(answer) if answer == 'X': break def main(): displaying_all() if __name__ == '__main__': main() RE: Trouble with Loops in lab assignment - jonstryder - Jul-28-2019 Thanks ThomasL, I'll type this in and see how it works. RE: Trouble with Loops in lab assignment - jonstryder - Jul-28-2019 So one problem I see with the code ThomasL is that I have no idea what "Format" is and how it is used. As well as the use of "end" in the coding you supplied. These are things we haven't covered in class yet. RE: Trouble with Loops in lab assignment - ThomasL - Jul-28-2019 1) end='' What happens if you run the print() function? There is nothing inside the parenthesis, so you tell python to print nothing but actually something happens on the screen. Yeah the cursor is going one line down because you print a "Carriage return/New line" character and this is due to the default parameter end='\n'. Just run help(print). So overriding this default by explicitly calling the print function with end='' you suppress the "going to the next line" and the cursor stays behind your last output. 2) print('Name:{} Street:{} Town:{}'.format(name, street, town)) The .format() is used to conveniently insert the value of variables into a string. You could see it as a type of concatenation. You could code it as print('Name:', name, 'Street:', street, 'Town:', town) Since Python 3.6 there are so-called f-strings which is an even more convenient type of string formatting. It would look like print(f'Name:{name} Street:{street} Town:{town}') where in all examples name, street and town are variables. I hope that helps you understand it. |