Python Forum
Needing help with something to do with my assignment.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Needing help with something to do with my assignment.
#1
I'm doing my GCSE OCR 20 hour coding task and have been told to just spend 20 hours on one of the 4 topics we could choose from. I chose to pick a 2 player dice game and am currently developing a way to authorise the 2 players, but am running into a problem to do with while loops. I'm really new to coding so there's probably a really straight forward answer, but I've been searching for upwards of 30 minutes and could not find a solution. I get the desired outcome if I enter something that's meant to be invalid, but then the same outcome for something that's meant to be right. This is my code:
whitelist = ["1", "2"]

A = 0
B = 5

while 'inputplayer1' not in whitelist:
    A += 1
    inputplayer1 = input("Player 1, please enter your name: ").lower()
    if 'inputplayer1' in whitelist:
        print("Player 1 is now authorised")
        found = True
    elif A == B:
        print("You ran out of attempts")
        break
    else:
        if 'inputplayer1' not in whitelist:
            print("Invalid username; please try again.")
As I said above, not matter what I enter, even if it is in the list, I get the response "Invalid username; please try again.".
The only thing I am asking of help here is a way to get the correct output for entering a correct answer, e.g. whats in the list
Reply
#2
'inputplayer1'? Seams odd in the comparison.
Reply
#3
You are comparing the string 'inputplayer1' to the values in the list '1' or '2', which will never compare equal.
Obviously you want to compare the variable inputplayer1 to the values in the list. So remove the quotes around the variable.
Reply
#4
@ibreeden is correct in that you are currently looking for the string 'inputplayer1' in whitelist instead of looking for what's contained by the variable inputplayer1. Also, once you remove the quotation marks, you will be referencing the variable before it's assigned in your input statement. I would go about it more like this if I were you.

whitelist = ["1", "2"]
B = 5

for attempts in range (B) :
	inputplayer1 = input("Player 1, please enter your name: ").lower()
	if inputplayer1 in whitelist:
		print("Player 1 is now authorised")
		found = True
		break
	else:
		if attempts < B -1 :
			print("Invalid username; please try again.")
		else :
			print("You ran out of attempts")
Reply
#5
Sounds like there may be a player 2. May as well write reusable code.
whitelist = ["1", "2"]

def input_player(prompt, whitelist):
    for _ in range (5):
        player = input(f'{prompt}: ')
        if player.lower() in whitelist:
            return player
    return None

if player1 := input_player('Player 1, please enter your name', whitelist):
    print(f'{player1} is now authorized')
else:
    print("You ran out of attempts")
ibreeden likes this post
Reply


Forum Jump:

User Panel Messages

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