3

so this is the code I wrote that attempts to answer the question in the title :

import random
print("Well, hello there.")
while True:
    a = random.randint(1,6)
    sum = 0
    if(a==1): #If a one is indeed rolled, it just shows rolled a 1 and 'Pigs out' and makes the score equal to 0(player score that is) and is a sort of a GameOver
        print("Pigged out!")
        break #To get out of the loop
    else:
        while(sum<=20): 
            sum += a
            print(sum)

The program should hold the score till it reaches 20(or more) and display it. It essentially represents a single turn of 'Pig'. I am unable to figure out where I am going wrong with this? Any suggestions would be helpful.

An example of a sample output:

-rolled a 6 -rolled a 6 -rolled a 5 -rolled a 6 -Turn score is 23

ggezpython3
  • 53
  • 2
  • 12

4 Answers4

1

If I understand rightly then you can simplify this quite a lot, like this:

import random
print("Well, hello there.")
score = 0
while score < 20:
    a = random.randint(1,6)
    print("A {} was rolled".format(a))
    score += a
    if a == 1:
        print("Pigged out!")
        score = 0
        break
print("Turn score is {}".format(score))
Stuart
  • 9,597
  • 1
  • 21
  • 30
  • Yes this works too! I love how simple and straightforward this is! Still learning how to simplify code as I am a beginner. Thanks! – ggezpython3 Sep 29 '18 at 22:18
0

If you just want to display the sum once it's greater than 20, shouldn't you unindent the print(sum) to the left? Essentially:

while(sum<=20): 
    sum += a
print(sum)

It would be nice if you could clarify what you want the output to be and what it is currently doing.

Ambidextrous
  • 810
  • 6
  • 14
  • It should roll till the sum is 20 or more and just display it. Now my program is rolling a lot of non-1s and just goes on until a 1 is rolled. How do I make it stop when the sum is 20? – ggezpython3 Sep 29 '18 at 21:24
0

You should break after the sum is more than 20

else:
    while(sum<=20): 
        sum += a
        print(sum)
    break

Edit:

import random
print("Well, hello there.")
while True:
    a = random.randint(1,6)
    sum = 0
    if(a==1): #If a one is indeed rolled, it just shows rolled a 1 and 'Pigs out' and makes the score equal to 0(player score that is) and is a sort of a GameOver
        print("Pigged out!")
        break #To get out of the loop
    else:
        if not SumWasReached:
           while(sum<=20): 
               a = random.randint(1,6)
               sum += a
               print(sum)
           SumWasReached ==True:
        else:
            while(a!=1):
               a = random.randint(1,6)
            break
user2853437
  • 750
  • 8
  • 27
  • Alright I tried indenting the code and adding the break statement and it works but it is rolling the same dice value? Like it displays rolled a 4 5 times or rolled a 6 4 times and shows the score. Is it some error or just 'random'? – ggezpython3 Sep 29 '18 at 21:30
  • 2
    It is rolling the numbers all over again because a = random.randint(1,6) was set outside the loop. So it doesn't dice again – user2853437 Sep 29 '18 at 21:48
  • 1
    @user285347 Thanks! Finally figured it out! – ggezpython3 Sep 29 '18 at 21:57
0

is this what you want?

import random
print("Well, hello there.")
sum=0
while True:
    a = random.randint(1,6)
    sum+=a
    if(a==1): #If a one is indeed rolled, it just shows rolled a 1 and 'Pigs out' and makes the score equal to 0(player score that is) and is a sort of a GameOver
        print("Pigged out!")
        break #To get out of the loop
    else:
        if sum<=20: 
            sum += a
            print(sum)
        else:
            print(sum,'limit reached')
            break
xenopus
  • 78
  • 8
  • The output should be something like `-rolled a 6 -rolled a 4` so on until the score is 20 or more at which point it should stop. If a 1 is rolled, the score becomes 0(game over).I added a print("- rolled a " + str(a)) to show the user what they rolled. – ggezpython3 Sep 29 '18 at 21:35