4 Replies - 617 Views - Last Post: 08 May 2016 - 09:47 PM Rate Topic: -----

#1 salwei35  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 08-May 16

I am trying to get my nested if else statement to work to display winn

Posted 08 May 2016 - 07:47 PM

I am trying to get my nested if else statement to work to display winners name either player one or two or tie. I can;t seem to get it
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
        winnersName = 'NO NAME'
        p1number = '0'
        p2number = '0'

        #call to rollDice
        winnerName = rollDice(p1number, p2number, playerOne, playerTwo, winnerName)

        #call to displayInfo
        displayinfo = 'winnerName'


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


   	#this function gets the players names
    def inputNames (playerOne, playerTwo):
        playerOne = raw_input ("Enter your name player1.")
        playerTwo = raw_input ("Enter your name player2.")
        return playerOne, playerTwo

#this function will get the random values
def rollDice(p1number, p2number, playerOne, PlayerTwo, winnerName):
    p1number = random.randint(1, 6)
    p2number = random.randint(1, 6)

if p1number > p2number
    print 'Player 1 wins" 
        elif p1number < p2number
            print 'Player 2 wins'
                else p1number = p2number
                    print ' You have tied'
End if

    

#this function displays the winner
def print_info(displayinfo)
    print "This is the winner", winnerName

# calls main
main()

This post has been edited by jon.kiparsky: 08 May 2016 - 08:07 PM
Reason for edit:: fixed [code] tags


Is This A Good Question/Topic? 0
  • +

Replies To: I am trying to get my nested if else statement to work to display winn

#2 Olegvin  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 34
  • Joined: 19-July 13

Re: I am trying to get my nested if else statement to work to display winn

Posted 08 May 2016 - 09:17 PM

I'm not sure, what is going on with the code to be honest it looks strange to say the least. But you are using If's as if it was some other language and not python.
I'm also not sure why It's nested, you are not getting into the same IF deeper, just doing the elif for different output and outcome.
if p1number > p2number:
    print('Player 1 wins")
elif p1number < p2number:
    print('Player 2 wins')     # remove the () if you use python 2.7
else p1number = p2number:
    print(' You have tied')



There is no need for End if and you haven't used the colons by the end of if
Was This Post Helpful? 1
  • +
  • -

#3 DK3250  Icon User is offline

  • Pythonian
  • member icon

Reputation: 402
  • View blog
  • Posts: 1,297
  • Joined: 27-December 13

Re: I am trying to get my nested if else statement to work to display winn

Posted 08 May 2016 - 09:34 PM

Line 5 in post #2 should only be
else:

Was This Post Helpful? 1
  • +
  • -

#4 Olegvin  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 34
  • Joined: 19-July 13

Re: I am trying to get my nested if else statement to work to display winn

Posted 08 May 2016 - 09:36 PM

Right sorry, I just fixed the indents and colons forgot to remove it.
Was This Post Helpful? 0
  • +
  • -

#5 ndc85430  Icon User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 798
  • View blog
  • Posts: 3,258
  • Joined: 13-June 14

Re: I am trying to get my nested if else statement to work to display winn

Posted 08 May 2016 - 09:47 PM

A few things:

1. Python has booleans (i.e. True and False) that you can use rather than strings like "no". Similarly, the value None exists, so you don't have to use strings like "NO NAME". None is falsy, so you can write things like

if not name:
    ....



2. On lines 19 and 20, you've got variables called p1number and p2number, but the values you're giving them are actually strings. I assume that's not what you wanted, given you're doing comparisons with them later on.

3. You don't do anything with that displayInfo variable.

4. Variable names are usually written with underscores (so called "snake case"), not in "camel case" - e.g. playerOne should be player_one.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1