help with loops in python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

help with loops in python

count = 0 while True: userName = input("Username: ") password = input("Password: ") count += 1 if count == 3: print('Too many login attempts. Goodbye.') break else: if userName == 'student' and password == 'abc123': print ("Login attempt sucessful!\n") print('\n''GAMES \n------------') print('1. Print the Pattern') print('2. Guess the Number') print('3. Logout') print ("\n\n") x=input ('>>Enter option: '); print('\n\n\n')#let them in while x in '1': print('Print the Pattern') z = input('Enter a whole number: ') n = int(z) s = input('Enter a symbol: ') for i in range(n): for j in range(i+1): print(s,end='') print() import random randAns = random.randint(0, 100) numberOfGuesses = 0 while x in '2': print('Guess the Number') # reading number from user ans = int(input('Between 0 and 100 : ')) print('\n') # incrementing numberOfGuesses numberOfGuesses += 1 # if user input matches the generated number # prints correct number and stops the loop # if user input is less it prints guess is less # else prints guess is greater than random number if ans == randAns: print('YOU GOT IT!') break elif ans < randAns: print('TOO LOW! \nGUESS AGAIN!') print('\n\n\n') elif ans > randAns: print('TOO HIGH! \nGUESS AGAIN!') print('\n\n\n') if x in '3': print('Goodbye. See you soon.') print('\n\n\n') else: print('Invalid Option')

11th Feb 2021, 10:33 PM
Gutz_X_73vEn
Gutz_X_73vEn - avatar
15 Answers
+ 4
What is the error ? What does program do ? For what input are you getting an error ? If you can add those in description as well , it might be easier for someone to help.
11th Feb 2021, 10:55 PM
Abhay
Abhay - avatar
+ 2
Gutz_X_73vEn copy paste the code in python file , and share the code link here as i think there is limitation on characters for description.
11th Feb 2021, 10:58 PM
Abhay
Abhay - avatar
+ 2
Gutz_X_73vEn it is working fine. The only problem i see is the 2 option where while loops forever.
11th Feb 2021, 11:26 PM
Abhay
Abhay - avatar
+ 2
Gutz_X_73vEn sorry, i meant 1st one , just use ( if x in '1' in place of while ) if you want user to print pattern once only.
12th Feb 2021, 12:09 AM
Abhay
Abhay - avatar
+ 2
oh perfect that makes since this was my first loop excercise thank you Abhay!
12th Feb 2021, 11:21 AM
Gutz_X_73vEn
Gutz_X_73vEn - avatar
+ 2
Gutz_X_73vEn also this code works 2 two times only because you are increasing count before "if count==3" , instead place it after "else". if count == 3: print('Too many login attempts. Goodbye.') break else: count+=1 if userName == 'student' and there are few other things as well that can be corrected , so i will edit it and let you know later on.
12th Feb 2021, 12:02 PM
Abhay
Abhay - avatar
+ 2
Abhay i tried sending in pm but wouldn’t send so i put link to videos here
13th Feb 2021, 6:25 PM
Gutz_X_73vEn
Gutz_X_73vEn - avatar
+ 1
oh sorry lol it cut out my explanation
11th Feb 2021, 10:57 PM
Gutz_X_73vEn
Gutz_X_73vEn - avatar
0
so its supposed to be a game library with 2 simple games 1 guess the number 2print the pattern and 3 is logout which should take back to the login menu i need it to give three attempts to login and display how many are left and if you go over 3 attempts i need it to say too many login attempts then i need the games to loop back to the main menu after playing once
11th Feb 2021, 10:59 PM
Gutz_X_73vEn
Gutz_X_73vEn - avatar
11th Feb 2021, 11:09 PM
Gutz_X_73vEn
Gutz_X_73vEn - avatar
0
sorry it looks kinda crappy on mobile lol
11th Feb 2021, 11:10 PM
Gutz_X_73vEn
Gutz_X_73vEn - avatar
0
i just need it to loop back to the select menu after each time you play a game
12th Feb 2021, 12:11 AM
Gutz_X_73vEn
Gutz_X_73vEn - avatar
0
oh thank you so much that really helps im trying to juggle too many classes at once but im going back over the basics again !!
12th Feb 2021, 12:25 PM
Gutz_X_73vEn
Gutz_X_73vEn - avatar
0
Instead of break use continue hope it would work
13th Feb 2021, 7:41 AM
Akhilesh Soni
Akhilesh Soni - avatar