Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I wanted to create a program that randomly assigns the user a group if the name is in the list and if not it should repromt the user but once i am done with the number of names in the lsit it should stop but it keeps on promting how should i get out of it.........





Python
  1  import random
  2  names=["ayush","bhargav","nayan","money","niju"]
  3  while True:
  4      for name in names:
  5          name=input("what is your name: ")
  6          name=name.casefold()
  7          group=random.randint(1,5)
  8          if name in names:
  9              print(name,"your group is",group)
 10          break
 11      else:
 12             continue


What I have tried:

tried using break but not working
Posted
Updated 9-Apr-23 7:54am
v3
Comments
0x01AA 9-Apr-23 13:31pm    
Now I'm far away from knowing phyton... but is it not only the break in line no. 10 which needs to be 'inserted' ?
How I hate languages depending on intendations :-)
Dave Kreskowiak 9-Apr-23 15:59pm    
The break statement gets you out of the inner-most loop, be it a "for" or "while". You would be much better off using a flag to get you out of the outer "while" loop.

The Break statement belongs to the preceding if, so it needs to be indented to the same level as the print.
Python
if name in names:
    print(name,"your group is",group)
    break


[edit]
A simplified solution is:
Python
import random
names=["ayush","bhargav","nayan","money","niju"]
while True:
    whoareyou=input("what is your name: ")
    if whoareyou in names:
        group=random.randint(1,5)
        print(whoareyou,"your group is",group)
        break
    else:
        print(whoareyou, "is not a valid name, try again")


[/edit]
 
Share this answer
 
v2
Comments
0x01AA 9-Apr-23 14:01pm    
Same idea here.. but I was unsure. Have my little 5.
[Edit]
And holy whatever...
also the else needs to be intended? How I hate that language. How i love it for numpy :)
Richard MacCutchan 10-Apr-23 4:07am    
The else statement is redundant, since continue is the default behaviour if the break is not called.
Use a flag in your while statement;

Python
active = True
while active:
.
.
.
  if name in names:
    print(name,"your group is",group)
    active = False
 
Share this answer
 
Comments
Prayash Bhuyan 9-Apr-23 23:54pm    
Hey the flag actually worked but not exactly i am able to break out of the loop but what i want is that when the name is not in the list the for loop shouldnot run it should just promt the user but here it considering it to be one iteration can you help?
Prayash Bhuyan 10-Apr-23 0:35am    
For doing what i intended to do i cehcked the if statement first so it is not considering it to be interation but now the problem is if one name is in ht elist it prints all the name together and doesnot ask the user for names again and again

import random
names=["ayush","bhargav","nayan","money","niju"]
active=True
while active:
name=input("what is your name: ")
if name in names:
for name in names:
name=name.casefold()
group=random.randint(1,5)
print(name,"your group is",group)
active=False
else:
continue
Richard MacCutchan 10-Apr-23 4:10am    
See my solution above, for the correct usage of the break statement. You can also remove the else and continue as they are not necessary.
Prayash Bhuyan 10-Apr-23 4:49am    
I got what you said but what i am saying is that that works fine but i want the for loop to run only when the name is in the lsit and if not it should repromt the user but the promting message shouldnot be counted as iteration but in my programe it is counting it as a iteration...
Prayash Bhuyan 10-Apr-23 4:50am    
Also once the for loop runs if the user enters a wrong input it should not also count that as an iteration

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900