Python help

I am having trouble with getting this to work properly

Page 1 of 1

4 Replies - 2340 Views - Last Post: 14 May 2010 - 01:58 PM Rate Topic: -----

#1 Guest_cyncrwler*


Reputation:

Python help

Posted 13 May 2010 - 10:00 AM

The link above code below is to a python program I am working on. I am obviously missing something, but for the life of me can't figure out what. I am stumped, and any input would be greatly appreciated.

This is to be a simple dice game, that rolls two dice, picks a random value, picks the winner or if it's a tie, says tie.

#Lab 7-3 The Dice Game
#add libraries needed
import random

#the main function
def main():
    print

    #initialize variables
    endProgram = 'no'
    playerOne = 'NO NAME'
    playerTwo = 'NO NAME'

    #call to inputName
    playerOne, playerTwo = inputNames (playerOne, playerTwo)


    #while loop to run program again
    while endProgram == 'no':
        winnerName = 'NO NAME'
        
        #initialize variables
        p1number = 0
        p2number = 0
        
        #call to roll dice
        winnerName = rollDice (p1number, p2number, playerOne, playerTwo, winnerName)


        #call to display info
        displayInfo = winnerName


        endProgram = raw_input ('Do you want to end the program?  (Enter yes or no): ')

        
#this function gets the players names
def inputNames():
    inputNames = raw_input('Please enter your names')
    playerOne, playerTwo = inputNames (playerOne, playerTwo)
    return playerOne, playerTwo


#this function will get the random values
def rollDice():
    p1number = random.randit (1, 6)
    p2number = random.randit (1, 6)
    if p1number >= p2number:
        winnerName = playerOne
    if p1number == p2number:
        winnerName = 'TIE'
    else winnerName = playerTwo    
        return winnerName
    



#this function displays the winner
def displayInfo():
    print 'The winners name is ', winnerName

#calls main
main()



This post has been edited by JackOfAllTrades: 13 May 2010 - 10:44 AM
Reason for edit:: Added code to post.


Is This A Good Question/Topic? 0

Replies To: Python help

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6036
  • View blog
  • Posts: 23,421
  • Joined: 23-August 08

Re: Python help

Posted 13 May 2010 - 10:45 AM

PROTIP: Don't make us download your code. Paste it into your post with code tags, like I did.

So tell us what the problems are. You're trying to make it hard on us to help you.
Was This Post Helpful? 0
  • +
  • -

#3 Guest_cyncrwler*


Reputation:

Re: Python help

Posted 13 May 2010 - 10:52 AM

Thanks for editing that, and sorry I didn't get it right the first time.

I am getting a syntax error on this lune:

else winnerName = playerTwo it comes up on winnerName.

I'm not understanding why the syntax, as I don't see the error there. I dont know what it wants from me to put there.

Does it want me to take out the winnerName altogether, and just go with, else = playerTwo?
Was This Post Helpful? 0

#4 programble  Icon User is offline

  • (cons :dic :head)

Reputation: 49
  • View blog
  • Posts: 1,315
  • Joined: 21-February 09

Re: Python help

Posted 14 May 2010 - 01:14 PM

= is for assignment. == is for comparison. Change the = to an ==.

This post has been edited by programble: 14 May 2010 - 01:14 PM

Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5777
  • View blog
  • Posts: 12,591
  • Joined: 16-October 07

Re: Python help

Posted 14 May 2010 - 01:58 PM

You're throwing all these variables around, but you're ignoring them half the time. There are a few places where this program would never run. I don't know what the hell inputNames is doing.

Here's a working program, please review it and ask any questions you like. Make sure you attempt to run your program before posting it; saves time.

import random

def main():
	playerOne, playerTwo = inputNames()
	
	endProgram = False
	while not endProgram:
		winnerNum = rollDice()
		winnerName = ('Tie', playerOne, playerTwo)[winnerNum]
		print 'The winners name is ', winnerName
		endProgram = raw_input('Do you want to end the program?  (Enter yes or no): ')=='yes'


def inputNames():
	playerOne = raw_input('Please enter the name of player 1')
	playerTwo = raw_input('Please enter the name of player 2')
	return playerOne, playerTwo


def rollDie():
	return random.randint(1, 6)


def rollDice():
	(p1, p2) = ( rollDie(), rollDie() )
	if (p1==p2):
		return 0 # tie
	elif (p1>p2):
		return 1
	else:
		return 2

main()



One big improvement to this would be to show what each player rolled. Also, the winner is bit a clumsy for a tie. Play around with it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1