Reputation: 9
So I have looked at all related questions and can't seem to find answers specific to my program. I have to have two player dice game that rolls a random number and declares the winner. As of now, when the program runs it says: The winner is: Enter player 1 name: Enter player 2 name:
Here is my code:
import random
def main():
print "You are now playing Dice Roll!!"
endProgram = 'no'
playerOne = 'NO NAME'
playerTwo = 'NO NAME'
playerOne, playerTwo = inputNames(playerOne, playerTwo)
while endProgram == 'no':
winnnerName = 'NO NAME'
p1number = '0'
p2number = '0'
playerOne,playerTwo = inputNames(playerOne,playerTwo)
winnerName =()
winnerName = rollDice(p1number, p2number, playerOne, playerTwo, winnerName)
displayInfo = winnerName
endProgram = raw_input('Do you want to end program? (Enter yes or no): ')
def inputNames(playerOne,playerTwo):
playerOne = raw_input('Enter player one name:')
playerTwo = raw_input('Enter player two name:')
return playerOne, playerTwo
def rollDice(p1number, p2number, playerOne, playerTwo, winnerName):
p1number = random.randint(1,6)
p2number = random.randint(1,6)
if p1number == p2number:
print "TIE!!!!"
elif p1number > p2number:
print winnnerName
else:
p1number < p2number
print winnnerName
return winnerName
print "The winner is:",winnerName
main()
I'm not looking for the answer just the guidance to go be on the right path. Thanks in advance.
Upvotes: 0
Views: 3541
Reputation: 91
Here are some steps that should help and guide you :
Be sure to indent your code correctly, as this is part of the python syntax. So lines with def ...():
should not be indented, definitions content should be indented of one level, while
content should be indented of two levels... And so on. So you should only have import
and the different def
aligned on the left (Maybe it happened when you pasted your code on stackoverflow, but just to be sure).
Stop passing arguments to every definition you make. If you do not need to pass a variable value to a function, don't use arguments. For instance, in def inputNames(playerOne,playerTwo)
, arguments playerOne,playerTwo
are useless because you're just using this function to define their value. The same problem appears in rollDice
.
Be careful about typos, you wrote "winnnerName" with 3 "n" in rollDice.
Keep it simple. Use only what is needed. Your code is well structured. But these functions should do only what they are intended to do. Example : rollDice
should only roll dices, and not announce the result. So you may just return the winner, or even return the numbers and test the winner in the main program. That bring us to the next part, the algorithm.
A good practice if you aren't comfortable with your code, is to write down your algorithm. It will be much clearer. So you should basically end up with something like this :
main function : the one called once when starting the program
inputNames : called to ask players names
rollDice : called to get random numbers
(3bis) rollDice alternative : called to get winner's name. Here you need to pass players names in argument
Now you can try yourself, you can certainly enhance it. Here is one I made, you can use it for reference if you get stuck : https://repl.it/BiKd/6.
Upvotes: 2