Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Cheri Castro
226 PointsMy roll the dice game isn't initiating, I'm not sure what I've done wrong, please help.
#add libraries needed
import random
#the main function
def main():
print
#initialize variables
endProgram = 'no'
playerOne = 'NO NAME'
playerTwo = 'NO NAME'
#call to inputNames
playerOne, playerTwo = inputNames(playerOne, playerTwo)
#while loop to run program again
while endProgram == 'no':
winnersName = 'NO NAME'
p1number = 0
p2number = 0
#initialize variables
p1number = 0
p2number = 0
winnersName = 'NO NAME'
#call to rollDice
winnersName = rollDice(p1number, p2number, playerOne, playerTwo, winnersName)
#call to displayInfo
displayInfo = winnersName
endProgram = input('Do you want to end program? (Enter yes or no): ')
#this function gets the players names
def inputNames(playerOne, playerTwo):
playerOne = input("Enter your name, player one: ")
playerTwo = input("Enter your name, player two: ")
return playerOne, playerTwo
#this function will get the random values
def rollDice(p1number, p2number, playerOne, playerTwo, winnersName):
p1number = int(random.randint(1, 6))
p2number = int(random.randint(1, 6))
if p1number == int(p2number):
winnersName = ("TIE")
elif p1number > int(p2number):
winnersName = ("playerOne")
else:
winnersName = playerTwo
return winnersName
#this function displays the winner
def displayInfo(winnersName):
print("The winner is: ", winnersName)
# calls main
main()
7 Answers
Ryan Cross
5,742 Pointscheck indent on that winners name return
Cheri Castro
226 PointsOk, it won't seem to let me move it. Neither to the left or the right.
Cheri Castro
226 PointsAh, I think I found the sweet spot. right under (lined up under the if, elif, else: statements). After I put it there, it finally returned a TIE statement as the winner. Thank you for your help!
Ryan Cross
5,742 Pointswhen you say
call to displayInfo
displayInfo = winnersName
do you mean displayInfo(winnersName)?
Cheri Castro
226 PointsOh yes, plus, I have changed some other things which got things to work but, player one never seems to win.
#add libraries needed
import random
#the main function
def main():
print
#initialize variables
endProgram = "no"
playerOne = "NO NAME"
playerTwo = "NO NAME"
#call to inputNames
playerOne, playerTwo = inputNames(playerOne, playerTwo)
#while loop to run program again
while endProgram == 'no':
#initialize variables
p1number = 0
p2number = 0
winnersName = 'NO NAME'
#call to rollDice
winnersName = rollDice(p1number, p2number, playerOne, playerTwo, winnersName)
#call to displayInfo
displayInfo(winnersName)
endProgram = input('Do you want to end program? (Enter yes or no): ')
#this function gets the players names
def inputNames(playerOne, playerTwo):
playerOne = input("Enter your name, player one: ")
playerTwo = input("Enter your name, player two: ")
return playerOne, playerTwo
#this function will get the random values
def rollDice(p1number, p2number, playerOne, playerTwo, winnersName):
p1number = random.randint(1, 6)
p2number = random.randint(1, 6)
if p1number == p2number:
winnersName = "TIE"
elif p1number > p2number:
winnersName = playerOne
else:
winnersName = playerTwo
return winnersName
#this function displays the winner
def displayInfo(winnersName):
print("The winner is: ", winnersName)
# calls main
main()
Cheri Castro
226 PointsI'm not sure if the other changes that I made actually made any difference or if it was just the same spot that you identified. But really curious as to why only player two or none wins, never player one.
Ryan Cross
5,742 Pointsyou bet! have fun and work hard!
Cheri Castro
226 PointsThe tabbing/spacing/indention issues are kicking my butt!
Chris Howell
Python Web Development Techdegree Graduate 49,703 PointsHey Cheri Castro
So one of the issues you are having has to do with your rollDice method.
def rollDice(p1number, p2number, playerOne, playerTwo, winnersName):
p1number = random.randint(1, 6) # lets pretend this rolls a 5
p2number = random.randint(1, 6) # and this rolls a 3
if p1number == p2number: # this equates to False
winnersName = "TIE" # never runs
elif p1number > p2number: # so we try this... This is True
winnersName = playerOne # This DOES run
else: # this does not run, because the elif above was True, this is a catch all if the previous was False.
# Basically this only should run if all the IFs are False so you should have your `return` outside the else.
winnersName = playerTwo
return winnersName
Cheri Castro
226 PointsThank you, I don't think I've viewed these as being true or false, not really. I had actually figured out the return statement and fixed it but the second time that I copied the code, perhaps I didn't do it quite correctly. The correct place for the return should have been under but lined up with the if, elif, and else statements. It was only stumbled across though. I had tried to the right and to the left, flush, and then one space at a time, when Ryan had stated to check it. I am still struggling to understand what all of the right rules are with Python regarding spacing, tabbing/indention's. I am so thankful for treehouse and the classes that are helping me learn as well as those who are willing to explain and give advice. Little nudges in the right direction.
Cheri Castro
226 PointsCheri Castro
226 PointsIt takes in the names of the players and then that's it, it asks me if I want to end the game.