Advertisement
Guest User

Untitled

a guest
Sep 20th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.08 KB | None | 0 0
  1. # 1. You've got an extra comma in all the function declarations which is not really recommended, it is not needed and may cause problems later:
  2.  
  3. #For example:
  4.  
  5. def rungame(num, number, guesses,) should be
  6. def rungame(num, number, guesses):
  7.  
  8. #2. You haven't quoted your 'y' and 'n' in the string comparison to see if they want to play again:
  9.  
  10.       if Ans == y:
  11.         return 7
  12.       elif Ans == n:
  13.         exit(0)
  14.  
  15. #should be
  16.  
  17.       if Ans == 'y':
  18.         return 7
  19.       elif Ans == 'n':
  20.         exit(0)
  21.  
  22. #3. Your 'y'/'n' check block isn't correctly indented, it should be indented to the same level of indentation as the 'else:' statement so that it is only executed when the player runs out of guesses.
  23.  
  24. #This:
  25.  
  26.    if guesses > 0:
  27.       Guesses(num, number, guesses)
  28.    else:
  29.       print("Sorry. You have run out of guesses. Would you like to try again?")
  30.       Ans = raw_input("y/n")
  31.  
  32.    if Ans == y:
  33.       return 7
  34.    elif Ans == n:
  35.       exit(0)
  36.  
  37. #should be:
  38.  
  39.    if guesses > 0:
  40.       Guesses(num, number, guesses)
  41.    else:
  42.       print("Sorry. You have run out of guesses. Would you like to try again?")
  43.       Ans = raw_input("y/n")
  44.  
  45.       if Ans == y:
  46.         return 7
  47.       elif Ans == n:
  48.         exit(0)
  49.  
  50. #4. You need a while 1: loop as part of your main() function. This ensures that the program will keep looping around and asking for guesses until it is exited. Currently your code will only ask for one guess and then exit.
  51.  
  52. #This:
  53.  
  54. def main():
  55. #This is the main module, which calls up each other sub module when it is needed.
  56.    print(" Hello. My name is El cubo. Im thinking of an number between 1-100. You have 7 guesses to try and guess the number. Think you can guess it?")
  57.    number = RandomNum()
  58.    guesses = 7
  59.    num = input("Please guess a number between 1-100: ")
  60.    numGuess(num, number)
  61.    rungame(num, number, guesses)
  62.  
  63. #Should be something similar to this:
  64.  
  65. def main():
  66. #This is the main module, which calls up each other sub module when it is needed.
  67.    print(" Hello. My name is El cubo. Im thinking of an number between 1-100. You have 7 guesses to try and guess the number. Think you can guess it?")
  68.    number = RandomNum()
  69.    guesses = 7
  70.    while 1: # infinite loop until the program is exited
  71.     num = input("Please guess a number between 1-100: ")
  72.     numGuess(num, number)
  73.     rungame(num, number, guesses)
  74.  
  75. #5. You need to update the guesses variable in main() with the updated number of guesses left after you check whether a guess is right. You should reassign the guesses variable to be the returned value of the runGuess function call, so that it will update the number of guesses to equal a fresh new set of 7 when #the player wants to continue.
  76.  
  77. def main():
  78. #This is the main module, which calls up each other sub module when it is needed.
  79.    print(" Hello. My name is El cubo. Im thinking of an number between 1-100. You have 7 guesses to try and guess the number. Think you can guess it?")
  80.    number = RandomNum()
  81.    guesses = 7
  82.    while 1:
  83.            num = input("Please guess a number between 1-100: ")
  84.            numGuess(num, number)
  85.            rungame(num, number, guesses)
  86.  
  87. #Should be:
  88.  
  89. def main():
  90. #This is the main module, which calls up each other sub module when it is needed.
  91.    print(" Hello. My name is El cubo. Im thinking of an number between 1-100. You have 7 guesses to try and guess the number. Think you can guess it?")
  92.    number = RandomNum()
  93.    guesses = 7
  94.    while 1:
  95.            num = input("Please guess a number between 1-100: ")
  96.            numGuess(num, number)
  97.            guesses = rungame(num, number, guesses)
  98.  
  99. #Also we need to fix the rungame function to return the existing number of guesses if there are guesses left.
  100.  
  101. def rungame(num, number, guesses):
  102. # This module will figure out if the game still has any guesses left.
  103. #If it doesn't, it will give the user a chance to play again. If the user
  104. #decides to play again, then they will be given 7 more guesses, and they
  105. #can play again.
  106.    if guesses > 0:
  107.       Guesses(num, number, guesses)
  108.  
  109. #changes to:
  110.  
  111. def rungame(num, number, guesses):
  112. # This module will figure out if the game still has any guesses left.
  113. #If it doesn't, it will give the user a chance to play again. If the user
  114. #decides to play again, then they will be given 7 more guesses, and they
  115. #can play again.
  116.    if guesses > 0:
  117.       return Guesses(num, number, guesses)
  118.  
  119. #6. The Guesses function is slightly wrong because it does not deduct the 1 from the amount of guesses left properly, also it doesn't return the new guesses count back to the calling function, which is rungame().
  120.  
  121. def Guesses(num, number, guesses):
  122. #This module will take away one guess if the answer is wrong.
  123.    if num < number:
  124.       guesses - 1
  125.    elif num < number:
  126.       guesses - 1
  127.  
  128. #Becomes:
  129.  
  130. def Guesses(num, number, guesses):
  131. #This module will take away one guess if the answer is wrong.
  132.    if num < number:
  133.       guesses = guesses - 1
  134.    elif num < number:
  135.       guesses = guesses - 1
  136.    return guesses
  137.  
  138. #That is all the problems. Once those are fixed, the resultant code runs fine and looks like this:
  139.  
  140. #<start corrected file>
  141.  
  142. import random
  143.  
  144. def rungame(num, number, guesses):
  145. # This module will figure out if the game still has any guesses left.
  146. #If it doesn't, it will give the user a chance to play again. If the user
  147. #decides to play again, then they will be given 7 more guesses, and they
  148. #can play again.
  149.    if guesses > 0:
  150.       return Guesses(num, number, guesses)
  151.    else:
  152.       print("Sorry. You have run out of guesses. Would you like to try again?")
  153.       Ans = raw_input("y/n")
  154.       if Ans == 'y':
  155.         return 7
  156.       elif Ans == 'n':
  157.         exit(0)
  158.  
  159. def numGuess(num, number):
  160. #This module will figure out if 'num' (the user's guess) is either larger or
  161. #smaller then the random number, or if it is correct. If it is higher or lower,
  162. #then the module should tell the user. If it is correct, the program should
  163. #congradulate the user.
  164.    if num < number:
  165.      print("Sorry, that's too low. Try again.")
  166.    elif num > number:
  167.      print("Sorry, that's too high. Try again.")
  168.    elif num == number:
  169.      print("Congratulations! you guessed the number! good job!")
  170.  
  171. def Guesses(num, number, guesses):
  172. #This module will take away one guess if the answer is wrong.
  173.    if num < number:
  174.       guesses = guesses - 1
  175.    elif num < number:
  176.       guesses = guesses - 1
  177.    return guesses
  178.      
  179. def RandomNum():
  180. #This module will, when the program starts up, find a random number between
  181. #1 and 100. This will choose a different number each time the program
  182. #starts up.
  183.    rndNum = random.randrange (1,100)
  184.    return rndNum
  185.  
  186. def main():
  187. #This is the main module, which calls up each other sub module when it is needed.
  188.    print(" Hello. My name is El cubo. Im thinking of an number between 1-100. You have 7 guesses to try and guess the number. Think you can guess it?")
  189.    number = RandomNum()
  190.    guesses = 7
  191.    while 1:
  192.        num = input("Please guess a number between 1-100: ")
  193.        numGuess(num, number)
  194.        guesses = rungame(num, number, guesses)
  195.    
  196. main()
  197. david@david-desktop:/tmp$ cat tryfour.py
  198. import random
  199.  
  200. def rungame(num, number, guesses):
  201. # This module will figure out if the game still has any guesses left.
  202. #If it doesn't, it will give the user a chance to play again. If the user
  203. #decides to play again, then they will be given 7 more guesses, and they
  204. #can play again.
  205.    if guesses > 0:
  206.       return Guesses(num, number, guesses)
  207.    else:
  208.       print("Sorry. You have run out of guesses. Would you like to try again?")
  209.       Ans = raw_input("y/n")
  210.       if Ans == 'y':
  211.         return 7
  212.       elif Ans == 'n':
  213.         exit(0)
  214.  
  215. def numGuess(num, number):
  216. #This module will figure out if 'num' (the user's guess) is either larger or
  217. #smaller then the random number, or if it is correct. If it is higher or lower,
  218. #then the module should tell the user. If it is correct, the program should
  219. #congradulate the user.
  220.    if num < number:
  221.      print("Sorry, that's too low. Try again.")
  222.    elif num > number:
  223.      print("Sorry, that's too high. Try again.")
  224.    elif num == number:
  225.      print("Congratulations! you guessed the number! good job!")
  226.  
  227. def Guesses(num, number, guesses):
  228. #This module will take away one guess if the answer is wrong.
  229.    if num < number:
  230.       guesses = guesses - 1
  231.    elif num < number:
  232.       guesses = guesses - 1
  233.    return guesses
  234.      
  235. def RandomNum():
  236. #This module will, when the program starts up, find a random number between
  237. #1 and 100. This will choose a different number each time the program
  238. #starts up.
  239.    rndNum = random.randrange (1,100)
  240.    return rndNum
  241.  
  242. def main():
  243. #This is the main module, which calls up each other sub module when it is needed.
  244.    print(" Hello. My name is El cubo. Im thinking of an number between 1-100. You have 7 guesses to try and guess the number. Think you can guess it?")
  245.    number = RandomNum()
  246.    guesses = 7
  247.    while 1:
  248.        num = input("Please guess a number between 1-100: ")
  249.        numGuess(num, number)
  250.        guesses = rungame(num, number, guesses)
  251.    
  252. main()
  253.  
  254. #<end corrected file>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement