Guess a number: Difference between revisions

From Computer Science Wiki
Line 66: Line 66:
<syntaxhighlight lang="python" >
<syntaxhighlight lang="python" >


list=[2,3,3,2,3,2,3,9,7,3,4,8,1,2,8,7,6,5,8,9,1,2,3,2,1,4,3,2,1,4,5,4,1,6,9,6,1,4,2,3,5]
import random
 
secret_number = random.randint(0, 100)
def mean(list):
guess = 0
    answer = sum(list) 
game = 1
     mean = answer / len(list)
level = raw_input("What level do you want(E, M, H, I): ")
    return mean
if level == "easy" or "e":
 
     l = 10
def mode(list):
elif level == "medium" or "m":
     frequency = {}
     l = 7
    highest = max(list)
elif level == "hard" or "h":
     lowest = min(list)
     l = 4
    # in this loop, we simply update our dictionary named "frequency with the count of values.
elif level == "impossible" or "i":
    for i in range(lowest,highest+1):
     l = 2
        frequency.update({i:list.count(i)})
while True:
     values = frequency.values()
 
    keys = frequency.keys()
     print("This is guess: " + str(guess))
     mode = keys[values.index(max(values))]
     print("This is game:" + str(game))
     return mode
     if guess >= l:
 
        print("You ran out of guesses!")
def median(list):
         break
    new_list = sorted(list)
    player_guess = input("Please make a guess: ")
     if len(new_list) % 2 == 1:
     guess = guess +1
         median = new_list[len(list)/2]
     return median
    
    
print("the mean of list is: " + str(mean(list)))
    if player_guess == secret_number:
print("the median of list is: " + str(median(list)))
        print("You won in " + str(guess) + " guesses!")
print("the mode of list is: " + str(mode(list)))
     
        play_again = raw_input("Do you want to play again? ")
        if play_again == "y" or play_again == "Y" or play_again == "yes":
            guess = 0
            game = game + 1
        break
    elif player_guess < secret_number:
        print("That is a bit to low")
    elif player_guess > secret_number:
        print("That is a bit to high")v


</syntaxhighlight>
</syntaxhighlight>


The example below includes standard deviation and a bar graph.
<syntaxhighlight lang="python" >
import matplotlib.pyplot as plt
list =[2,3,3,2,3,2,3,9,7,3,4,8,1,2,8,7,6,5,8,9,1,2,3,2,1,4,3,2,1,4,5,4,1,6,9,6,1,4,2,3,5,5]
numbers = [1,2,3,4]
def graph(graph_list):
    count = [1]
    sorted_list = sorted(graph_list)
    highest = max(sorted_list)
    lowest = min(sorted_list)
    for i in range(lowest,highest+1):
        count.append(i)
    plt.hist(sorted_list, bins=count)
    plt.ylabel('Occurences')
    plt.xlabel('Number')
    plt.show()
 
def mean(mean_list):
    mean = sum(numbers)/len(numbers) 
    return mean
def mode(mode_list):
    frequency = []
    count = []
    sorted_list = sorted(mode_list)
    highest = max(sorted_list)
    lowest = min(sorted_list)
    for i in range(lowest,highest+1):
        frequency.append(sorted_list.count(i))
        count.append(i)
    highest_frequency = max(frequency)
    index_count = frequency.index(highest_frequency)
    mode = count[index_count]
    return mode
def median(median_list):
  sorted_list = sorted(median_list)
  if len(sorted_list)%2 ==1:
    index = len(sorted_list)/2
    median = sorted_list[index]
  else:
      index_1 = len(sorted_list)/2
      index_2 = len(sorted_list)/2 - 1
      median = []
      median.append(sorted_list[index_1])
      median.append(sorted_list[index_2])
      median = mean(median)
      print("The median is rounded up")         
  return median
def standev(stdev_list):
    mean_list = []
    mean_num = mean(stdev_list)
    for i in range (0,len(stdev_list)):
        mean_list.append(stdev_list[i]-mean_num)
    mean_difference = mean(mean_list)
    stdev = mean_difference**1/2
    return stdev
 
# your program must return the correct answers for the questions below:
print("the mean of list is: " + str(mean(numbers)))
print("the median of list is: " + str(median(numbers)))
print("the mode of list is: " + str(mode(numbers)))
print("the standard deviation of list is: " + str(standev(numbers)))
graph(numbers)
</syntaxhighlight>
The code below approaches mode differently than the two examples above (which use a dictionary).
<syntaxhighlight lang="python" >
list=[2,3,3,2,3,2,3,9,7,3,4,8,1,2,8,7,6,5,8,9,1,2,3,2,1,4,3,2,1,4,5,4,1,6,9,6,1,4,2,3,5]
def mean(list):
    mean = sum(list)/len(list) 
    return mean
def median(list):
    new_list = sorted(list)
    median = new_list[len(new_list)/2]
    return median
 
def mode(list):
    current_top = 0
    highest = max(list)
    lowest = min(list)
    for i in range(lowest,highest+1):
        new_possible_top = list.count(i)
        if new_possible_top > current_top:
            current_top = new_possible_top
            mode = i
    return mode
 
# your program must return the correct answers for the questions below:
print("the mean of list is: " + str(mean(list)))
print("the median of list is: " + str(median(list)))
print("the mode of list is: " + str(mode(list)))


</syntaxhighlight>
</syntaxhighlight>
Line 212: Line 117:
[[Category:python]]
[[Category:python]]
[[Category:strings]]
[[Category:strings]]
[[Category:math]]

Revision as of 15:52, 5 April 2016

This a problem set for you to work through [1]

This is a problem set. Some of these are easy, others are far more difficult. The purpose of these problems sets are to HELP YOU THINK THROUGH problems. The solution is at the bottom of this page, but please don't look at it until you have tried (and failed) at least three or four times.


What is this problem set trying to do[edit]

You have to think about conditions and computational thinking here. You are also going to be incrementing variables.

The Problem[edit]

Please write a simple game that randomly selects a number between 0 and 100. A player must guess the number. Your program should tell the player if their guess is higher or lower than the secret number.

  1. Your program must generate a random number between 0 and 100 and assign that random number to a variable.
  2. Your program must ask the player to guess a number
  3. Your program must compare the players guess and the secret number. The program should tell the player if their guess is higher or lower than the secret number
  4. Your program must tell the player if they have won
  5. Your program must ask the player if they want to play again

Some Code to Get You Started[edit]

This code will not run. If you see ... I have left out code, and you have to figure out what to put in.

import random
secret_number = ...
guess = 0
game = 1
  
    print("This is guess: " + str(guess))
    print("This is game:" + str(game))
    player_guess = input("Please make a guess: ")
    guess = guess +1
   
...
       
        play_again = raw_input("Do you want to play again? ")
        if play_again == "y" or play_again == "Y" or play_again == "yes":
            guess = 0
            game = game + 1
        break
 ....

Take This Further[edit]

  1. add in a condition that players only have a certain number of turns to win
  2. add in a difficulty level; easy, medium and expert. If a player has easy, they have 8 tries, medium, they have only 4 tries, and expert, only 2 tries!!

How you will be assessed[edit]

Every problem set is a formative assignment. Please click here to see how you will be graded

References[edit]

A few different possible solutions[edit]

Click the expand link to see one possible solution, but NOT before you have tried and failed!

import random
secret_number = random.randint(0, 100)
guess = 0
game = 1
level = raw_input("What level do you want(E, M, H, I): ")
if level == "easy" or "e":
    l = 10
elif level == "medium" or "m":
    l = 7
elif level == "hard" or "h":
    l = 4
elif level == "impossible" or "i":
    l = 2
while True:
   
    print("This is guess: " + str(guess))
    print("This is game:" + str(game))
    if guess >= l:
        print("You ran out of guesses!")
        break
    player_guess = input("Please make a guess: ")
    guess = guess +1
   
    if player_guess == secret_number:
        print("You won in " + str(guess) + " guesses!")
       
        play_again = raw_input("Do you want to play again? ")
        if play_again == "y" or play_again == "Y" or play_again == "yes":
            guess = 0
            game = game + 1
        break
    elif player_guess < secret_number:
        print("That is a bit to low")
    elif player_guess > secret_number:
        print("That is a bit to high")v


</syntaxhighlight>