Lists

What we’ve had to do for this assignment

creative commons licensed (BY-NC-SA) flickr photo by Mark Morgan Trinidad A: http://flickr.com/photos/mmorgan8186/5946796450
creative commons licensed (BY-NC-SA) flickr photo by Mark Morgan Trinidad A: http://flickr.com/photos/mmorgan8186/5946796450

Create a program that asks the user for 10 numbers  (floating point). Store those numbers in a list. Show to the user the total, average and standard deviation of those numbers.

Details

For Python3 you want to be using Lists here.

Once you have this working, change it so that users keep giving you values until they signal “no more values”. How would you implement this?

 

list1

 

list2

Factorial Calculator

What we’ve had to do this week:

creative commons licensed (BY-SA) flickr photo by ▓▒░ TORLEY ░▒▓: http://flickr.com/photos/torley/3505324528creative commons licensed (BY-SA) flickr photo by ▓▒░ TORLEY ░▒▓: http://flickr.com/photos/torley/3505324528

Create a program that asks the user for a non-negative integer (let’s call that number n) and display for them the value of n! (n factorial).

After showing them the answer, ask them if they would like to try another number (with a simple y/n response) and either ask again (for y) or quit the program and wish them a nice day (if they answered n).

Details

Since you are implementing this in Python3, resist the urge to call math.factorial(n). Yes that would solve the problem but what would we do if there was no math.factorial() and we had no internet to find someone’s solution?

There are two basic approaches: a loop with an accumulator of the multiplication and a recursive solution. Choose one and implement that. Once that is done, try the other way. If you used a while loop for the solution with a loop, try structuring this with a for loop (or vice-versa).

How I solved the assignment with my Mac:

Program I used: PyCharmCE

  1. First I had to create a function that receives a non-negative integer and returns the value of that non-negative factorial value.

# This function receives a non-negative integer and returns the value of that non-negative factorial value.

def factorial(n):
counter = n
result = 1

The function is defined by the command def. In order to get the factorial number I used a while loop. This loop is running until the factorial value of n is calculated.

In this loop counter is assigned as n, because we still want to keep the „original n“, which was the integer number that the user inserted. With doing so, the programmer is able to understand, which number was used to calculate the factorial number and he can use this number for another function as well. Another thing that we did was assigning result a 1. Why? Because in that function we are multiplying and 1 is the magic number with which you can multiply anything getting no chances in the result (for example: 1*1 = 1, 2*2 = 2).

After I’ve created those assignments, it was to create the actual loop:

while (counter > 1):
result = result * counter
counter = counter – 1
return result

With return result we ask the computer to output the result, which is the factorial value of n. This result can be used in print in order to show the user the result.

2. Next, it was to ask the user to enter an integer number and to show him the result afterwards:

print(„Enter a number of choice to get the factorial: „)
n = int(input())
print(„The factorial of your chosen number „, n, “ is „, factorial(n), „.“)

3. In the assignment we should as well ask the user if he wants to play again. The question should be answered with yes or no. It was clear that I had to create another while loop for this issue. But for me it was difficult to think about a solution. How could I let the computer run the loop again asking the first question? At the end it wasn’t complicated at all. The only thing I had to do was to put the first question and the result at the beginning, then reassign the input and then define the conditions. That was it!

while True:
print(„Enter a number of choice to get the factorial: „)
n = int(input())
print(„The factorial of your chosen number „, n, “ is „, factorial(n), „.“)

again = input(„Do you want to play again? Please enter ‚yes‘ or ’no‘.“)

if again == „yes“:
print („Fun (: Let’s do this again!“)

else:
print(„Okay, have a nice day.“)
break

 

The whole code looks like this:

fc1

When I’m running this it looks like this:

fcrun

 

After I’ve finished with that code, I’ve tried to replace the while loop with a for-loop. I tried to solve the problem without any webhelp and only with the knowledge I had about the for loop. After a little while of thinking I got it! The rest of the code looks the same and it works perfectly.

 

 

____________________________________________________

What were my lessons learned for this week?

Don’t code when you’re tired. This easiest thing can seem so difficult for you when you are tired. Yesterday, I was trying to solve the assignment for hours and I couldn’t do it. The next day, I got back to the assignment and everything made sense. So instead of wasting your time while being tired, go to sleep and get back to the assignment when you have a clear mind.

On To Functions

Why do we need functions?

creative commons licensed (BY) flickr photo by kevin dooley: http://flickr.com/photos/pagedooley/8435953365creative commons licensed (BY) flickr photo by kevin dooley: http://flickr.com/photos/pagedooley/8435953365

 

In Germany, we have a so called „Pfandsystem“ for plastic bottles. Every time you buy a plastic bottle, you pay about 25 cents as a deposit and when you return the plastic bottle, you get your deposit back and the bottle is being recycled and reused again. At first glance, this approach sounds like the perfect solution for recycling: less plastic bottles on the street and a clean environment for our next generations. However, it turned out that only 30 percent of the water and juice bottles that were sold were for multi-use in 2017. So even though the approach of recycling was for a good purpose, the system was not well established. It was a major issue, for instance, that the public could not distinguish between the bottles for multi-use and those for single-use.

So what can we learn from it?

As a programmer, you need to put a lot of effort into establishing a stable system. It is very important to create a code that is easy to read and understandable. Because if not, we have seen it with the bottles, you can not get the best out of your system. Programmers or even yourself won’t be able to work on the code later on, because most probably you would not understand what you did before. So defining functions helps in order to get structure to your code making it readable and reusable. That means once you’ve made that pre-work, you are profiting from it in the long-run. Additionally, having functions defined, it is a lot easier for you to write your code, since you can use a definition as a key instead of large functions.

 

What was our task for this assignment?

You will go back and do WSQ01 – Fun with Numbers again.

But this time, write a function for each calculation. Each function should define two parameters (in this example of type int) and return the correct value as an integer as well.

You main program needs to ask the user for the input and then call each function to calculate the answer for each of the parts.

 

How did I solved it with my Mac?

Program that I used: PyCharm CE

 

  1. First of all, I wrote down all of the functions that are required:
def addition(x,y):
    return x + y

def subtraction(x,y):
    return x - y

def multiply(x,y):
    return x * y

def divide(x,y):
    return x / y

def remainder(x,y):
    return x % y

2. Then, I assigned the computer to ask for the two integer numbers:
print("Insert two integer numbers. ")
x =int(input("Enter the first number: "))
y =int (input("Enter the second number: "))

3. In order to simplify the output of the result, I created values for the respective functions:

# RESULTS FOR PRINTING
r_add = str(addition(x,y))
r_sub = str(subtraction(x,y))
r_multi = str(multiply(x,y))
r_divi = str(divide(x,y))
r_rem = str(remainder(x,y))

4. At the end, I assigned the system to print out the results. 

print("The result of", x , "+", y, "is", r_add + ".")
print("The result of", x , "-", y, "is", r_sub + ".")
print("The result of", x , "*", y, "is", r_multi + ".")
print("The result of", x , "/", y, "is", r_divi + ".")
print("The result of", x , "%", y, "is", r_rem + ".")

5. The whole code looks like the following:

def addition(x,y):
    return x + y

def subtraction(x,y):
    return x - y

def multiply(x,y):
    return x * y

def divide(x,y):
    return x / y

def remainder(x,y):
    return x % y


print("Insert two integer numbers. ")
x =int(input("Enter the first number: "))
y =int (input("Enter the second number: "))


# RESULTS FOR PRINTING
r_add = str(addition(x,y))
r_sub = str(subtraction(x,y))
r_multi = str(multiply(x,y))
r_divi = str(divide(x,y))
r_rem = str(remainder(x,y))


print("The result of", x , "+", y, "is", r_add + ".")
print("The result of", x , "-", y, "is", r_sub + ".")
print("The result of", x , "*", y, "is", r_multi + ".")
print("The result of", x , "/", y, "is", r_divi + ".")
print("The result of", x , "%", y, "is", r_rem + ".")

6. This is how the program’s run looks like:

 

Sum of Numbers

What we’ve had to do for this assignment:

 

 

 

 

 

 

 

 

Write a program that asks for a range of integers and then prints the sum of the numbers in that range (inclusive).

You can use a formula to calculate this of course but what we want you to do here is practice using a loop to do repetitive work.

For example, the sum from 6 to 10 would be 0 + 6 + 7 + 8 + 9 + 10.

Notice our sum starts with zero (why?) and then we add each number in the range provided by the user. Just for fun, what is the mathematical formula to do this calculation?
Example Run

We will calculate the sum of integers in the range you provide. Please give us the lower bound:  1 Please give us the upper bound: 10 The sum from 1 to 10 (inclusive) is: 55

This is how I solved the assignment:

Program that I used: PyCharm CE

1. I wanted to give the user the possibility to enter a range of integer numbers. For that, I defined two inputs: one for the start and one for the end:

print(„We want to calculate the sum of a range from to integer numbers.“)

start = int(input(„Please enter your starting number: „))

end = int(input(„Please enter your ending number: „))

2.  Then I’ve added a for-loop to sum the range of the numbers:

final_sum = 0

for next_one in range(start,end +1):
    final_sum= final_sum + next_one

3. At the end, I assigned the computer to print the result

print("Your answer is: ", final_sum

4. This is how the code and the program looks like:

print(„We want to calculate the sum of a range from to integer numbers.“)

start = int(input(„Please enter your starting number: „))
end = int(input(„Please enter your ending number: „))

final_sum = 0

for next_one in range(start, end + 1):
final_sum = final_sum + next_one

print(„Your answer is: „, final_sum)

5. This is how the program looks like:


		

Pick a Number

This was the assignment we had to do this week:

What to Do

https://www.flickr.com/photos/dasprid/8147975983/

Write a program that picks a random integer in the range of 1 to 100.

There are different ways to make that happen, you choose which one works best for you.

It then prompts the user for a guess of the value, with hints of ’too high’ or ’too low’ from the program.

The program continues to run until the user guesses the integer. You could do something extra here including telling there user how many guesses they had to make to get the right answer.

You might want to check that your program doesn’t always use the same random number is chosen and you should also split your problem solving into parts. Perhaps only generate the random number and print that as a first step.

Example Run

I have a number chosen between 1 and 100.
Please guess a number between 1 and 100:  50
I’m sorry but 50 is too high, try again: 25
I’m sorry but 25 is too low, try again: 42
You got it! The right answer is indeed 42.
You made 3 guesses to get the right number.

 

This is the way in which I solved the problem:

This week I tried to use the PyCharm CE software instead of Atom, because PyCharm CE provides the ability to run the program directly on the app.

So the only prerequirement I needed was PyCharm CE.

What I’ve learned from the past assignments was that the best way to program is to write little pieces of code and then check if it works or not.

So I did the following:

  1. On the web, I’ve searched for a command that enables the computer to select a random integer number between 1 and 100. This the code that I found and that worked for me:
    import random
    
    n=(random.randint(1,101))
    
    The import part is here to insert a range from 1 to 101 in the code, because it is the range is exclusive.
  2. After that I wanted to give the user the option to guess a random number between that range.
    print("Please guess a number between 1 and 100.")
    x =int(input("Enter the number: "))
  3. Then I’ve tried to insert a loop function with the command „while“.
    while  n !=number:

    I’ve used this to assign the computer to process the loop when the guessed number does not equal the random number.

  4. Next, I told the computer that there are two conditions. The first one when the guessed number is higher than the random number and vice versa. For the later, I only used the function else, which was the easiest way for me.
    if n > number:
    
    else:
  5. Then I needed to define what the computer is to print if one of those conditions is true:
    
    print("I'm sorry, but" , number, "is too low. Please try again.")
    
    number =int(input("Enter the number: "))
    
    or:
    
    print("I'm sorry, but" , number, "is too high. Please try again.")
    
    number =int(input("Enter the number: "))

6. After that I wanted to integrate a function that provide the number of tries to the user. For that, first I inserted the following assignment in the loop:

count = 1

7. In both conditions I added the following for the tries to be counted:

count = count + 1

8. After that, I added the sentence for the result. While I wrote the sentence I realized that it could be that the user guesses the answer at the first time. So I wanted to integrate two conditions in order to match to the grammatics with the form of the singular and plural of the word „time“ or „times“.

if count == 1:
    print("You've got it!", number, "is the right number! You've tried 1 time!")

else:
    print("Not bad, you've got it!", number, "is the right number! You've tried", count, "times!")

9. That was it. The whole code looks like that and it works!

pickanumber

 


UPDATE!

In class today we’ve learned that the assignment random.randint isn’t enough to produce random numbers, because it is possible that the most probably the same sequence of random numbers is going to be produced in the next run. So, I’ve added a seed that is based on time to create pseudo random numbers from 1 to 100.

 

import random
from datetime import time

random.seed(time)
n =(random.randint(1, 100))