Python Forum
How to repeat input line of code until condition is met
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to repeat input line of code until condition is met
#1
So right now, I'm trying to loop a line that requires input until a condition is met, but I can't seem to do so.

Essentially, what I want is:

Code asks user how many players are playing: Var (playerAmt)
From there, set a while loop that while 'playerAmt'!= actualPlayers
Ask user for names of each player in relation to playerAmt: Var (playerNames) - e.g. if 3 players are playing, code will repeat input code until it's been asked 3 times, then break out of loop
Which will then added to a list using playerList.append(playerNames)

def playerNames():
    userNun=int(input("Number of players?"))
    while userAmount != userNun:
        userNames=input("Please input your names")
        playerList.append(userNames)
        random.shuffle(playerList)
        return playerList
I know this code isn't working, as I can't seem to find a way to arrange it in a way as stated above, any help will be appreciated, thanks!
Edit: The "userAmount" variable was asked before this function runs, which is obviously incorrect, but I have no idea how to remedy this.
Reply
#2
import random

def playerNames():
    playerList = []
    userNun=int(input("Number of players?\n> "))
    while len(playerList) != userNun:
        userNames=input("Please input name of player number {}/{}\n> ".format(len(playerList)+1, userNun))
        playerList.append(userNames)
    random.shuffle(playerList)
    return playerList

print(playerNames())
Output:
Number of players? > 2 Please input name of player number 1/2 > Michal Please input name of player number 2/2 > Monday ['Monday', 'Michal'] >>>
Reply
#3
>>> def input_with_cond(display, condition):
...   while True:
...     value = input(display)
...     yield value
...     if condition():
...       break
...
>>> num_players = int(input("How many? "))
How many? 3
>>> players = []
>>> for player in input_with_cond("Next player: ", lambda: len(players) >= num_players):
...   players.append(player)
...
Next player: joe
Next player: fred
Next player: moe
>>> players
['joe', 'fred', 'moe']
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Some line code explanation Chrilo06 3 2,070 Feb-24-2022, 06:24 PM
Last Post: deanhystad
  Create code for input names and score for 15 student Davin 2 2,043 Sep-21-2020, 08:49 AM
Last Post: DeaD_EyE
  Repeat keywords at end of every line knob 4 2,293 Sep-15-2020, 06:38 AM
Last Post: perfringo
  Repeat question (for loop) rs74 7 6,474 Jun-17-2020, 03:17 PM
Last Post: rs74
  Python code unable to show Bokeh line chart kirito85 2 2,504 Feb-06-2019, 07:52 AM
Last Post: kirito85
  Code to use output from arduino as input (help) Updownrightleft 0 2,231 Nov-03-2018, 11:04 PM
Last Post: Updownrightleft

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020