Python3 programming part 1

Python3 programming part 1

Pythonlogo

 

I enrolled in a python3 beginner course and will also be posting some work I've done for the course. I'll skip some of the more basic stuff. Some things written here like certain bits of console output will be in Finnish again due to some course requirements. The VIOPE system that we submit our code in to expects everything to be word for word the same as has been put in it.

Hello World in Python3

 

print("Hei Maailma!")

print("Tämä on Python-ohjelma")

 

 

A simple calculator that adds two numbers together.

 

print("Laskin")

number1 = int(input("Anna ensimmäinen luku: "))

number2 = int(input("Anna toinen luku: "))

print("Tulos on: ", int(number1+number2))

 

A simple question and answer feed that checks if user input is correct and either accepts the input if correct and if not the feed displays an error message

 

name = input("Anna nimi: ")

if (name == "Erkki"):

password = input ("Anna salasana: ")

if (password == "Esimerkki"):

print("Salasana ja nimi oli oikein!")

else:

print("Salasana oli väärin")

else:

print("Nimi oli väärin")

 

A console program that checks whether the number the user inputs is even or not.

 

number = int(input("Anna luku: "))

if (number % 2 == 0):

print("Antamasi luku oli parillinen.")

A console program that checks two user given numbers and gives feedback based on the numbers (if they are even or not). The way my program checks if the given number is even or odd is based off the remainder of dividing the number by two.

 

number = int(input("Anna luku: "))

number2 = int(input("Anna toinen luku: "))

if (number % 2 == 0) and (number2 % 2 == 0):

print("Molemmat luvut ovat parillisia.")

elif (number % 2 == 0) and (number2 % 2 > 0):

print("Toinen luku on parillinen.")

elif (number % 2 > 0) and (number2 % 2 == 0):

print("Toinen luku on parillinen.")

elif (number % 2 > 0) and (number2 % 2 > 0):

print("Molemmat luvut ovat parittomia.")

A console program that works as a calculator for two numbers and has the user select which basic method of calculation the calculator uses addition, subtraction, division or multiplying the numbers.

 

number = int(input("Anna ensimmäinen luku: "))

number2 = int(input("Anna toinen luku: "))

print("(1) +")

print("(2) -")

print("(3) *")

print("(4) /")

choice = int(input("Tee valinta (1-4): "))

if (choice == 1):

print("Tulos on: ", number + number2)

elif (choice == 2):

print("Tulos on: ", number - number2)

elif (choice == 3):

print("Tulos on: ", number * number2)

elif (choice == 4):

print("Tulos on: ",float(number / number2))

else:

print("Valintaa ei tunnistettu.")