Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
while loop question
#1
Hello, When my program runs and reaches the winning condition of else: it will print both from the if and else as well as subtract the 1 as if it was a losing value.
Need help with the order of operations.

an example of the outcome
Output:
what is your name?keys Hello! keys you rolled a 6 loser your cash is 99 you rolled a 8 loser your cash is 98 you rolled a 9 loser your cash is 97 you rolled a 7 loser your cash is 96 [color=#1ABC9C]you rolled a 11 loser your cash is 95 you rolled a 11 winner! your cash is 195[/color]
#dice game

name = input("what is your name?")
print ("Hello!",name)

import random
die1 = random.randint(1, 6)
die2 = random.randint(1, 6)
total = die1 + die2
money = 100
while total <10:
    if total <10:
        die1 = random.randint(1, 6)
        die2 = random.randint(1, 6)
        total = die1 + die2
        money -=1
    print ("you rolled a",total)
    print ("\tloser")
    print ("your cash is",money)

else:
        money +=100
        print("you rolled a",total)
        print("\twinner!")
        print("your cash is",money)
Reply
#2
First of all, I don't understand why you have both 11 and 12. It seems like you don't need both.

But your big problem is that you're changing the total after testing it, but before doing something with it.

Imagine you got a 5 and entered the if portion. Then you change total to something else (like 11). it then removes 1 and prints "loser"without caring that total is now greater.
Reply
#3
Thanks for the reply,
With your notes i was able to re order the code for a working result xd
Thanks again.
#dice game

name = input("what is your name?")
print ("Hello!",name)


total = 0
money = 100
import random    
while money >=0:
    input("Press Enter to roll")
        
    die1 = random.randint(1, 6)
    die2 = random.randint(1, 6)
    total = die1 + die2
    money -=1
    if total <10:
        money -=20
        print ("you rolled a",total)
        print ("\tloser")
        print ("your cash is",money)

    elif total >=10:
        money +=100
        print("you rolled a",total)
        print("\twinner!")
        print("your cash is",money)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  A question about 'Event loop is closed' fc5igm 2 2,209 Oct-05-2021, 02:00 AM
Last Post: fc5igm
Exclamation question about input, while loop, then print jamie_01 5 2,674 Sep-30-2021, 12:46 PM
Last Post: Underscore
  for loop question KEYS 1 1,729 Oct-27-2020, 11:42 PM
Last Post: jefsummers
  Netmiko Loop question sc00ter 2 3,323 Oct-24-2020, 10:54 PM
Last Post: sc00ter
  New to programming, loop question tomyan 1 1,638 Sep-25-2020, 04:32 PM
Last Post: Larz60+
  while loop question spalisetty06 2 1,852 Aug-13-2020, 04:18 PM
Last Post: buran
  question about for loop Than999 5 2,485 Jun-09-2020, 02:16 PM
Last Post: Emekadavid
  Question about for loop not creating an infinite loop. FWendeburg 1 2,119 Feb-03-2019, 08:45 PM
Last Post: ichabod801
  Loop Condition Question malonn 6 3,478 Aug-01-2018, 01:56 PM
Last Post: malonn
  Beginner Loop question BigDisAok 5 3,733 Jul-24-2018, 02:04 PM
Last Post: BigDisAok

Forum Jump:

User Panel Messages

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