Stanford Python Camp D – 5

Share:

D-5: Using more advanced loop commands with Python operators

Assignment 5 – A. Sum

Write a program that requests a user to enter a sequence of numbers continuously until the user enters 0. The program should find the sum and average of the numbers entered.

Assignment 5 – A code
Assignment 5 – A outcome

Assignment 5 – B. Max and Min

Write a program that requests a user to enter a sequence of numbers continuously until a negative number is entered. The program should print the highest and lowest positive entered.

Assignment 5 – B code
Assignment 5 – B outcome

Assignment 5 – C. Guessing Game

Write a program for a guessing game. The program should first generate a random number between 1 and 100 and then prompt the user to guess the number. If the user guesses correctly, the program should print that the guess is ‘correct’. If the guess is lower than the generated number it should print ‘too low’ and if the guess is higher, then it should print ‘too high’ and then request the user to guess again until the user guesses correctly. The program should also print the number of guesses taken by the user.

Assignment 5 – C code
Assignment 5 – C outcome

Assignment 5 – D. Dice Game Pig

Write a program for the dice game Pig. The object of the game is to be the first one to reach 100. The user and the computer will play against each other. The user and the computer can take turns playing the game as follows:

• On each turn each player (the user or the computer) will roll two dice. Use a random number generator to simulate the rolling of the dice.

  • If no 1 appears, the dice values are added to a running total for the turn. Then the player can either decide to pass or roll again. If the player passes, then the running total is added to the player’s grand total, and the next player rolls the dice.
  • If a 1 appears on any one of the dice, the players running total is reset to 0 and the next player rolls the dice.
  • If 1 appears on both dice, then the grand total reset to 0, and the next player rolls the dice.

• For the computer to decide when to roll again or pass, generate a random number between 1 and 10. If the number of 5 or below, roll again, and if the number greater than 5, then pass the turn to the player.

Assignment 5 – D code
Assignment 5 – D outcome

Stay In Touch.

  • kimhannah2005@gmail.com