1

Sorry if this is considered spam, yet another issue I have is that this while loop runs unconditionally and I am not sure how to fix it.

Code:

while anotherNum == True:
    playerNum = random.randint(1,10)
    total = total + playerNum
    print("")
    print("Your number is ", str(playerNum) + ".")
    print("You have " + str(total) + " in total.")
    print("")
    again = input("Roll another number? <Y or N> ")
    print("")
    if again == "Y":
      anotherNum == True
    else:
      anotherNum == False
      break
  #game finished now
print("Computer got", pcNum)
print("You got", total)
#checking for winner
while anotherNum == False:
  if (total <= 13) and (total > pcNum): 
    print("You,", name, "have won!")
  elif (pcNum <= 13) and (pcNum > total):
    print("The computer has bested you,", name + "!")
  else:
    if (total == pcNum) and (total <= 13):
      print("Draw...")
    elif (pcNum > 13) and (total <= 13):
      print("You,", name + " have won!")
    else:
      print("Both of you have lost. Wow...")

Output:

Your number is  2.
You have 2 in total.

Roll another number? <Y or N> n


Your number is  3.
You have 3 in total.

Roll another number? <Y or N> N


Your number is  3.
You have 3 in total.

Roll another number? <Y or N> N


Your number is  9.
You have 9 in total.

Instead of going to the #game finished now comment's area, the while loop repeats the process whilst only disregarding the total = total + playerNum command.

4 Answers 4

2

You are not changing the valu

if again == "Y":
 anotherNum == True
 else:
 anotherNum == False
 break

You should use 1 equal sign as it is an assignment

if again == "Y":
 anotherNum = True
 else:
 anotherNum = False
 break
Sign up to request clarification or add additional context in comments.

Comments

1

Your code (with minimal correction) is working just fine:

import random
total = 0
anotherNum = True

while anotherNum == True:
    playerNum = random.randint(1,10)
    total = total + playerNum
    print("")
    print("Your number is ", str(playerNum) + ".")
    print("You have " + str(total) + " in total.")
    print("")
    again = input("Roll another number? <y or n> ")
    print("")
    if again == "y":
        anotherNum = True
    else:
        anotherNum = False
        break
print("You got", total)

The output that I get after running the code:

Your number is  7.
You have 7 in total.

Roll another number? <y or n>  y


Your number is  9.
You have 16 in total.

Roll another number? <y or n>  y


Your number is  10.
You have 26 in total.

Roll another number? <y or n>  y


Your number is  2.
You have 28 in total.

Roll another number? <y or n>  y


Your number is  6.
You have 34 in total.

Roll another number? <y or n>  n

You got 34

Comments

0

Here's how I would do it.

while anotherNum:
    playerNum = random.randint(1,10)
    total += playerNum
    print("")
    print("Your number is ", str(playerNum) + ".")
    print("You have " + str(total) + " in total.")
    print("")
    again = input("Roll another number? <Y or N> ")
    print("")
    anotherNum = (again == "Y")

this way you can avoid the if <blank> then true else false which caused you to make a mistake here (simple == instead of =)

2 Comments

I still have the same issue.
Are you sure that you're inputing "Y" , not "y" ? That will cause issues... Try to print again to make sure ? And please note that the first = is only one character and the second is "=="
0

Try to remove break and change == to =

import random

total = 0
anotherNum = True

while anotherNum == True:
    playerNum = random.randint(1, 10)
    total += playerNum
    print("")
    print(f"Your number is {playerNum}.")
    print(f"You have {total} in total.")
    print("")
    again = input("Roll another number? <Y or N> ")
    print("")
    if again == "Y":
        anotherNum = True  # <- HERE
    else:
        anotherNum = False  # <- HERE

OR without anotherNum variable:

import random

total = 0

while True:
    playerNum = random.randint(1, 10)
    total += playerNum
    print("")
    print(f"Your number is {playerNum}.")
    print(f"You have {total} in total.")
    print("")
    again = input("Roll another number? <Y or N> ")
    print("")
    if again == 'N':
        break

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.