20,263 views
35 votes
35 votes
What's wrong with my program? Here is my code , if the user wants a 3rd card dealt they, have to select yes and the total will be for 3 cards. If they don't, then the total is for 2 cards.? I also need to print the total for both the dealer and the user and determine the winner of the game?

# import random
import random
def main():
# greet the user
print("Hello! Welcome to my card game!")
# deal the user a random card between 1 and 10
num1 = random.randint(1,10)
print("You drew a", num1)
# deal the user another random card (between 1 and two)
num2 = random.randint(1,10)
print("You drew a", num2)
num3 = random.randint(1,10)

# deal the user a third card?
again = input("Would you like to draw a 3rd card? Y or N? ")
d_total = random.randint(10,21)
u_total = (num1) + (num2) + (num3)
u_total1 = (num1) + (num2)
if again == "Y" or "y":

print("You drew a", num3)


if (u_total > d_total) and (u_total < 21):
# the user wins
print("Congratulations! You won!")
print("You drew a",num1,",", "a",num2,"," "and a",num3, "for a total of",u_total)
if again == "N" or "n":
print("You drew a", num1,"and a",num2, "for a total of",u_total1)


if (u_total > d_total) and (u_total < 21):


print("The dealer's total is", d_total)




else:

print("Sorry! You lose.")

main()

User Nourish
by
2.9k points

1 Answer

6 votes
6 votes

Your issue is on line 19 in your if statement. In python, if you have more than one condition in an if statement, you have to explicitly mention it after the or.

Your if statement is

if again == "Y" or "y":

However, this will always return true because the second statement simply asks "y".

To correct this, simply change the if statement to:

if again == "Y" or again == "y":

This will correct your code.

Another thing to consider is to always convert a userinput (whenever possible) to one version, this can be accomplished in your code by converting "again" into one version by using the .lower function.

again = input("Would you like to draw a 3rd card? Y or N? ")

again = again.lower()

Hope this helps!

User Sonnie
by
2.2k points