Code doesnt work as expected to


#1


https://www.codecademy.com/courses/python-ext/projects/number-guess

When I ask the program second time' wanna play again'? and enter 'Y' as input. It doesnt call roll_dice() function again. Instead, it prints 'you are free to leave'..

I expect the function to roll dice again which it doesnt!!


"""This program rolls a pair of dice and adds both values then asks user to guess the number and compares it to original value. If the user's roll is greater than total value of dice roll, user wins and vice versa. Also inform user"""


from random import randint
from time import sleep

def get_user_guess():
  	user_guess = int(raw_input('Guess number'))
  	return user_guess

def roll_dice():
  number_of_sides= int(raw_input('please enter number of sides a dice should have'))
  
  print 'Rollingaaaa....'
 
  sleep(3)
  
  first_roll= randint(1,number_of_sides)
  second_roll= randint(1, number_of_sides)
  roll_dice= first_roll + second_roll
  max_val= number_of_sides *2
  
  print'The max possible value is from 1 to ' + str(max_val)

  my_guess= get_user_guess()
  
  print 'waiting for your luck to decide.. hehehe'
  
  if my_guess> max_val:
      print 'Guess invalid'  
      return
  else:
      print'The first roll is %d' % first_roll
      print'The second roll is %d' % second_roll
      print'The total is %d ' % roll_dice
      if my_guess>roll_dice:
          print'you won'
      else:
          print'you lost' 
          return
            
roll_dice()

sleep(2)


second_ask = str(raw_input('wanna play again?'))

if second_ask == 'y or Y':
    roll_dice()
else: 
    print'you are free to leave'



#2

if you want to play again, you now have to enter y or Y, that doesn't make much sense

or shouldn't be part of the string, its a keyword in python and you have to compare at both sides of the or operator


#3

you are a life saver!


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.