Issues with stopping for loop

hey guys- brand new to the forum, let me know if i’m not doing something correctly.
When the player loses I want it to not ask the player if they want to draw another card.

import random
lose = 'You Lose!'
point = 'You Got a Point!'
numPoints = 0
numloops = [0]
for i in numloops:
    cards = [point, point, point, lose]
    random.shuffle(cards)
    print(cards)
    PChoice = input('Which card do you want to choose?(1, 2, 3 or 4): ')
    if (PChoice == '1' and cards[0] == point):
        numPoints += 1
    if (PChoice == '2' and cards[1] == point):
        numPoints += 1
    if (PChoice == '3' and cards[2] == point):
        numPoints += 1
    if (PChoice == '4' and cards[3] == point):
        numPoints += 1
    if (PChoice == '1'):
        print(cards[0])
    if (PChoice == '2'):
        print(cards[1])
    if (PChoice == '3'):
        print(cards[2])
    if (PChoice == '4'):
        print(cards[3])
    if (PChoice != '1' and PChoice != '2' and PChoice != '3' and PChoice != '4'):
        print('Please Input 1, 2, 3, 4.')    
    quit = input('Would you like to guess a card(yes/no): ')
    if (quit == 'no'):
        print('your score is,')
        print(numPoints)
        print('if you got one of the top ten high scores please show Avery.')
    if (quit == 'yes'):
        numloops.append(0)
    if (quit != 'yes' and quit != 'no'):
        print('Please type yes or no.')
    if (PChoice == '1' and cards[0] == lose):
        numloops.clear
    if (PChoice == '2' and cards[1] == lose):
        numloops.clear
    if (PChoice == '3' and cards[2] == lose):
        numloops.clear
    if (PChoice == '4' and cards[3] == lose):
        numloops.clear

clear is a method, so you need to call it to have it clear the list: numloops.clear().

But a better way to break out of a for loop is to use break:

for i in numloops:
    ...
    if you_want_to_stop_looping:
        break
1 Like

this is very helpful, thank you for taking your time to give me advice!

1 Like

I am having problems placing the promt “quit = input('Would you like to guess a card(yes/no): ')”, do you know where I would put it?

import random
lose = 'You Lose!'
point = 'You Got a Point!'
numPoints = 0
numloops = [0]
for i in numloops:
    cards = [point, point, point, lose]
    random.shuffle(cards)
    PChoice = input('Which card do you want to choose?(1, 2, 3 or 4): ')
    if (PChoice == '1' and cards[0] == point):
        numPoints += 1
    if (PChoice == '2' and cards[1] == point):
        numPoints += 1
    if (PChoice == '3' and cards[2] == point):
        numPoints += 1
    if (PChoice == '4' and cards[3] == point):
        numPoints += 1
    if (PChoice == '1'):
        print(cards[0])
    if (PChoice == '2'):
        print(cards[1])
    if (PChoice == '3'):
        print(cards[2])
    if (PChoice == '4'):
        print(cards[3])
    if (PChoice != '1' and PChoice != '2' and PChoice != '3' and PChoice != '4'):
        print('Please Input 1, 2, 3, 4.')    
    if (PChoice == '1' and cards[0] == lose):
        numloops.clear()
    if (PChoice == '2' and cards[1] == lose):
        numloops.clear()
    if (PChoice == '3' and cards[2] == lose):
        numloops.clear()
    if (PChoice == '4' and cards[3] == lose):
        numloops.clear()
    quit = input('Would you like to guess a card(yes/no): ')
    if (quit == 'no'):
        print('your score is,')
        print(numPoints)
        print('if you got one of the top ten high scores please show Avery.')
    if (quit == 'yes'):
        numloops.append(0)
    if (quit != 'yes' and quit != 'no'):
        print('Please type yes or no.')

It looks like it’s already in the correct place. You just need to add break after the print statements of the if statement.

1 Like

The problem is that after the player loses, it still asks if they would like to pick a card or not, sorry for not clarifying!

I just fixed it, but thank you!