'Python is not running my codes in else statement
I am doing the first Hog project for CS61A, a UC Berkeley Intro CS course that publishes all its materials online, to self-study CS(Link:https://inst.eecs.berkeley.edu/\~cs61a/sp20/) For its first question, it asks me to implement a function that basically rolls a dice for a specific number of times and returns the sum of all the numbers, unless one of the rolls gives number 1, in which case the function will return 1.
I have two versions of codes. There are issues with the first one, but the second version passed the test:
First Version:
def roll_dice(num_rolls, dice=six_sided):
"""Simulate rolling the DICE exactly NUM_ROLLS > 0 times. Return the sum of
the outcomes unless any of the outcomes is 1. In that case, return 1.
num_rolls: The number of dice rolls that will be made.
dice: A function that simulates a single dice roll outcome.
"""
# These assert statements ensure that num_rolls is a positive integer.
assert type(num_rolls) == int, 'num_rolls must be an integer.'
assert num_rolls > 0, 'Must roll at least once.'
# BEGIN PROBLEM 1
"*** YOUR CODE HERE ***"
# END PROBLEM 1
sum, k = 0, 1
pig_out = False
while k <= num_rolls:
curr = dice()
if curr == 1:
pig_out = True
else:
sum += curr
k += 1
if pig_out:
return 1
else:
return sum
Second Version:
def roll_dice(num_rolls, dice=six_sided):
"""Simulate rolling the DICE exactly NUM_ROLLS > 0 times. Return the sum of
the outcomes unless any of the outcomes is 1. In that case, return 1.
num_rolls: The number of dice rolls that will be made.
dice: A function that simulates a single dice roll outcome.
"""
# These assert statements ensure that num_rolls is a positive integer.
assert type(num_rolls) == int, 'num_rolls must be an integer.'
assert num_rolls > 0, 'Must roll at least once.'
# BEGIN PROBLEM 1
"*** YOUR CODE HERE ***"
# END PROBLEM 1
sum, k = 0, 1
pig_out = False
while k <= num_rolls:
curr = dice()
if curr == 1:
pig_out = True
sum += curr
k += 1
if pig_out:
return 1
else:
return sum
In the first one, if I run roll_dice(1, make_test_dice(2, 3, 4)) in python intepreter, it gives me 3, while it should give me 2 (make_test_dice() is returns a dice that gives numbers in a pre-specified order. For example, make_test_dice(2, 3, 4) will give 2 if I run make_test_dice(2, 3, 4)() once, 3 if I run it 2 times, and 4 if I run it 3 times). It seems that Python will skip the first number if I run it only once, but I don't know why. I suspect it has something to do with the else statement, but I am not sure why it will cause such issues.... Any information will be hight appreciated!
I specified what I tried in the body of the question.
Solution 1:[1]
Doesn't seem possible for you to get different results. You probably had a different setup when you ran it? Unless you share the test case we can't really help much.
Your code seems unnecessarily complicated. This is simpler:
def roll_dice(num_rolls, dice=six_sided):
"""Simulate rolling the DICE exactly NUM_ROLLS > 0 times. Return the sum of
the outcomes unless any of the outcomes is 1. In that case, return 1.
num_rolls: The number of dice rolls that will be made.
dice: A function that simulates a single dice roll outcome.
"""
# These assert statements ensure that num_rolls is a positive integer.
assert type(num_rolls) == int, 'num_rolls must be an integer.'
assert num_rolls > 0, 'Must roll at least once.'
# BEGIN PROBLEM 1
"*** YOUR CODE HERE ***"
# END PROBLEM 1
sum = 0
for _ in range(num_rolls):
curr = dice()
if curr == 1:
return 1
sum += curr
return sum
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Anters Bear |