Compare commits
15 Commits
1e6640a00a
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 5c13e24abf | |||
| 7202fef78a | |||
| a033a62532 | |||
| 4b987606c2 | |||
| caeb8808be | |||
| 4df0cd11d2 | |||
| ddeeb712d8 | |||
| 3f70296387 | |||
| cb7e872c22 | |||
| 9c54280f1e | |||
| 01a24d18e7 | |||
| 1552cf5fdf | |||
| b8dce8fe6e | |||
| 260b94e218 | |||
| ee1c344da7 |
31
Defining_funtions.py
Normal file
31
Defining_funtions.py
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
def hello(name):
|
||||||
|
print(f'Hello {name}! How are you?')
|
||||||
|
|
||||||
|
username = input('What is your name stranger?\n>')
|
||||||
|
hello(username)
|
||||||
|
|
||||||
|
# Another example using functions and return code:
|
||||||
|
|
||||||
|
import random
|
||||||
|
|
||||||
|
# Defining the function:
|
||||||
|
def get_answer(answer):
|
||||||
|
if answer <10:
|
||||||
|
return "The answer is smaller than 10"
|
||||||
|
elif answer == 10:
|
||||||
|
return "The answer is 10."
|
||||||
|
else:
|
||||||
|
return 'The answer is greater than 10'
|
||||||
|
|
||||||
|
# Generating a random number, using that random number to define a variable by using it as an argument in the funtion,
|
||||||
|
# and printing the result:
|
||||||
|
random_number = random.randint(1, 20)
|
||||||
|
result = get_answer(random_number)
|
||||||
|
print(result)
|
||||||
|
|
||||||
|
# The 3 lines above can be rewritten to a single line:
|
||||||
|
print(get_answer(random.randint(1, 20)))
|
||||||
|
|
||||||
|
# And the check to see if the code is working as it should:
|
||||||
|
print(f"(It is {random_number})")
|
||||||
|
|
||||||
31
Return_codes_Magic_8_ball.py
Normal file
31
Return_codes_Magic_8_ball.py
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
import random
|
||||||
|
|
||||||
|
# This input only makes the user think the program reacts to his question!
|
||||||
|
# In reality it doesn't do anything!
|
||||||
|
|
||||||
|
input("Ask your question. Be sure it can be answered with a YES or NO!\n>")
|
||||||
|
|
||||||
|
# Define function and based on the evaluation define return code:
|
||||||
|
|
||||||
|
def get_answer(random_number):
|
||||||
|
if random_number == 1:
|
||||||
|
return 'It is certain!'
|
||||||
|
elif random_number == 2:
|
||||||
|
return 'It is decidedly so.'
|
||||||
|
elif random_number == 3:
|
||||||
|
return 'Yes'
|
||||||
|
elif random_number == 4:
|
||||||
|
return "Doubtful"
|
||||||
|
elif random_number == 5:
|
||||||
|
return 'Not likely'
|
||||||
|
elif random_number == 6:
|
||||||
|
return 'Mhhh, not sure about that one'
|
||||||
|
elif random_number == 7:
|
||||||
|
return "Nope, afraid not"
|
||||||
|
else:
|
||||||
|
return "No way in hell"
|
||||||
|
|
||||||
|
|
||||||
|
r = random.randint(1,8)
|
||||||
|
fortune = get_answer(r)
|
||||||
|
print(fortune)
|
||||||
8
change_global_var_from_within_function.py
Normal file
8
change_global_var_from_within_function.py
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
global_var = "var1"
|
||||||
|
|
||||||
|
def change_global_var():
|
||||||
|
global global_var
|
||||||
|
global_var = 'var2'
|
||||||
|
|
||||||
|
change_global_var()
|
||||||
|
print(global_var)
|
||||||
10
error_handling.py
Normal file
10
error_handling.py
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
def spam(divide_by):
|
||||||
|
try:
|
||||||
|
return 42 / divide_by
|
||||||
|
except ZeroDivisionError:
|
||||||
|
print('Error: Invalid argument.')
|
||||||
|
|
||||||
|
print(spam(2))
|
||||||
|
print(spam(1))
|
||||||
|
print(spam(42))
|
||||||
|
print(spam(0))
|
||||||
3
future_projects.md
Normal file
3
future_projects.md
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
Some ideas for future challenges learning ptyhon:
|
||||||
|
|
||||||
|
- black jack
|
||||||
11
playground.py
Normal file
11
playground.py
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
import random
|
||||||
|
for i in range(100): # Perform 100 coin flips.
|
||||||
|
if random.randint(0, 1) == 0:
|
||||||
|
print('H', end=' ')
|
||||||
|
else:
|
||||||
|
print('T', end=' ')
|
||||||
|
print()
|
||||||
|
|
||||||
|
|
||||||
|
# print('Deze print eindigt met een spatie', end=" ")
|
||||||
|
# print("Dit staat er naast")
|
||||||
125
pomodoro.py
Normal file
125
pomodoro.py
Normal file
@@ -0,0 +1,125 @@
|
|||||||
|
import time
|
||||||
|
import os
|
||||||
|
print("""
|
||||||
|
Welkom bij de pomodoro timer!
|
||||||
|
|
||||||
|
Deze timer zorgt voor het volgende schema om ononderbroken een taak uit te voeren:
|
||||||
|
|
||||||
|
- 25 minuten studie
|
||||||
|
- 5 minuten rust
|
||||||
|
- 25 minuten studie
|
||||||
|
- 5 minuten rust
|
||||||
|
- 25 minuten studie
|
||||||
|
- 15 tot 30 minuten rust
|
||||||
|
|
||||||
|
Regels:
|
||||||
|
|
||||||
|
1) Geen afleiding, geen social media of mail.
|
||||||
|
2) Komt er iets tussendoor? Noteer het en ga door met je taak.)
|
||||||
|
3) Een blok van 25 minuten is heilig en niet onderbreekbaar!
|
||||||
|
|
||||||
|
Druk op enter om de 1e sessie te starten.
|
||||||
|
""")
|
||||||
|
|
||||||
|
input()
|
||||||
|
|
||||||
|
if os.name == 'nt': # Windows
|
||||||
|
os.system('cls')
|
||||||
|
else: # Mac/Linux
|
||||||
|
os.system('clear')
|
||||||
|
|
||||||
|
runs = 0
|
||||||
|
keer = 1
|
||||||
|
|
||||||
|
print("Aftellen van 25 minuten:")
|
||||||
|
while runs != 3:
|
||||||
|
|
||||||
|
|
||||||
|
study_counter = 1500
|
||||||
|
pause_counter = 300
|
||||||
|
|
||||||
|
while study_counter != 0:
|
||||||
|
timeshow_minute = int(study_counter / 60 )
|
||||||
|
timeshow_second = int(study_counter % 60)
|
||||||
|
print(timeshow_minute,":",timeshow_second, end='\r')
|
||||||
|
time.sleep(1)
|
||||||
|
study_counter -= 1
|
||||||
|
if keer == 1:
|
||||||
|
print("Dit was het 1e blok van 25 minuten.\nDruk op enter om 5 minuten te pauzeren.")
|
||||||
|
keer += 1
|
||||||
|
input()
|
||||||
|
if os.name == 'nt': # Windows
|
||||||
|
os.system('cls')
|
||||||
|
else: # Mac/Linux
|
||||||
|
os.system('clear')
|
||||||
|
elif keer == 2:
|
||||||
|
print("Dit was het 2e blok van 25 minuten.\nDruk op enter om 5 minuten te pauzeren.")
|
||||||
|
keer += 1
|
||||||
|
input()
|
||||||
|
if os.name == 'nt': # Windows
|
||||||
|
os.system('cls')
|
||||||
|
else: # Mac/Linux
|
||||||
|
os.system('clear')
|
||||||
|
else:
|
||||||
|
print("Dit was het 3e en laatste blok van 25 minuten.\nDruk op enter om af te sluiten.")
|
||||||
|
input()
|
||||||
|
break
|
||||||
|
|
||||||
|
|
||||||
|
while pause_counter != 0:
|
||||||
|
timeshow_minute = int(pause_counter / 60 )
|
||||||
|
timeshow_second = int(pause_counter % 60)
|
||||||
|
print(timeshow_minute,":",timeshow_second, end='\r')
|
||||||
|
time.sleep(1)
|
||||||
|
pause_counter -= 1
|
||||||
|
print("Je pauze is voorbij. Druk op enter als je door wil gaan met het volgende blok")
|
||||||
|
input()
|
||||||
|
|
||||||
|
runs += 1
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# runs = 0
|
||||||
|
# run = 1
|
||||||
|
|
||||||
|
# while runs <= 3:
|
||||||
|
# pauze = 0
|
||||||
|
# seconds = 0
|
||||||
|
# rest_time = 300
|
||||||
|
# while seconds <= 1500:
|
||||||
|
# print(rest_time)
|
||||||
|
# time.sleep(1)
|
||||||
|
# seconds += 1
|
||||||
|
# rest_time -= 1
|
||||||
|
# print(f"""Er zijn 25 minuten voorbij! Run{run}Neem nu 5 minuten pauze zonder afleiding!""")
|
||||||
|
|
||||||
|
# while pauze <= 10:
|
||||||
|
# print(pauze)
|
||||||
|
# time.sleep(1)
|
||||||
|
# pauze += 1
|
||||||
|
# if runs != 3:
|
||||||
|
# print("De pauze is voorbij. Druk op enter om het volgende blok van 25 minuten te starten")
|
||||||
|
# input()
|
||||||
|
# runs += 1
|
||||||
|
|
||||||
|
# print("3 runs zijn voorbij")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
140
rock-paper-scissors.py
Normal file
140
rock-paper-scissors.py
Normal file
@@ -0,0 +1,140 @@
|
|||||||
|
#############################################
|
||||||
|
# #
|
||||||
|
# Done! #
|
||||||
|
# #
|
||||||
|
#############################################
|
||||||
|
|
||||||
|
import random
|
||||||
|
import sys
|
||||||
|
import time
|
||||||
|
import os
|
||||||
|
import platform
|
||||||
|
|
||||||
|
if platform.system() == "Windows":
|
||||||
|
os.system('cls')
|
||||||
|
else:
|
||||||
|
os.system('clear')
|
||||||
|
losses = 0
|
||||||
|
wins = 0
|
||||||
|
ties = 0
|
||||||
|
|
||||||
|
while True:
|
||||||
|
|
||||||
|
if platform.system() == "Windows":
|
||||||
|
os.system('cls')
|
||||||
|
else:
|
||||||
|
os.system('clear')
|
||||||
|
|
||||||
|
|
||||||
|
print(f'''We are going to play a game of Rock, Paper, Scissors!
|
||||||
|
losses: {losses} wins: {wins} ties: {ties}''')
|
||||||
|
|
||||||
|
|
||||||
|
# Computer choice:
|
||||||
|
|
||||||
|
computer_choice = random.randint(1,3)
|
||||||
|
if computer_choice == 1:
|
||||||
|
computer_choice = 'Paper'
|
||||||
|
elif computer_choice == 2:
|
||||||
|
computer_choice = 'Rock'
|
||||||
|
elif computer_choice == 3:
|
||||||
|
computer_choice = 'Scissors'
|
||||||
|
|
||||||
|
# Player choice:
|
||||||
|
player_choice = input('What do you choose? Rock(R), Paper(P), or Scissors(S)?\n>')
|
||||||
|
if player_choice == 'P' or player_choice == 'p':
|
||||||
|
player_choice = 'Paper'
|
||||||
|
elif player_choice == 'R' or player_choice == 'r':
|
||||||
|
player_choice = 'Rock'
|
||||||
|
elif player_choice == 'S' or player_choice == 's':
|
||||||
|
player_choice = 'Scissors'
|
||||||
|
else:
|
||||||
|
print(f'The choice you made ("{player_choice}") is not valid. The program will now exit')
|
||||||
|
sys.exit()
|
||||||
|
|
||||||
|
# Playing the game:
|
||||||
|
|
||||||
|
if computer_choice == player_choice:
|
||||||
|
print('It is a tie!')
|
||||||
|
ties += 1
|
||||||
|
rematch = input('Would you like to play another game? (Y/N)\n>')
|
||||||
|
if rematch == 'y' or rematch == 'Y':
|
||||||
|
continue
|
||||||
|
elif rematch == 'n' or rematch == 'N':
|
||||||
|
print(f'''OK, thanks for playing! The final score:
|
||||||
|
wins: {wins}, losses: {losses}, ties: {ties}''')
|
||||||
|
sys.exit()
|
||||||
|
elif computer_choice == 'Rock':
|
||||||
|
if player_choice == 'Scissors':
|
||||||
|
print('You lost, you chose Scissors and the computer chose rock!')
|
||||||
|
losses += 1
|
||||||
|
rematch = input('Would you like to play another game? (Y/N)\n>')
|
||||||
|
if rematch == 'y' or rematch == 'Y':
|
||||||
|
continue
|
||||||
|
elif rematch == 'n' or rematch == 'N':
|
||||||
|
print(f'''OK, thanks for playing! The final score:
|
||||||
|
wins: {wins}, losses: {losses}, ties: {ties}''')
|
||||||
|
sys.exit()
|
||||||
|
elif player_choice == 'Paper':
|
||||||
|
print('You won, you chose scissors and the computer chose rock!')
|
||||||
|
wins += 1
|
||||||
|
rematch = input('Would you like to play another game? (Y/N)\n>')
|
||||||
|
if rematch == 'y' or rematch == 'Y':
|
||||||
|
continue
|
||||||
|
elif rematch == 'n' or rematch == 'N':
|
||||||
|
print(f'''OK, thanks for playing! The final score:
|
||||||
|
wins: {wins}, losses: {losses}, ties: {ties}''')
|
||||||
|
sys.exit()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
elif computer_choice == 'Paper':
|
||||||
|
if player_choice == 'Scissors':
|
||||||
|
print('You won, you chose Scissors and the computer chose paper!')
|
||||||
|
wins += 1
|
||||||
|
rematch = input('Would you like to play another game? (Y/N)\n>')
|
||||||
|
if rematch == 'y' or rematch == 'Y':
|
||||||
|
continue
|
||||||
|
elif rematch == 'n' or rematch == 'N':
|
||||||
|
print(f'''OK, thanks for playing! The final score:
|
||||||
|
wins: {wins}, losses: {losses}, ties: {ties}''')
|
||||||
|
sys.exit()
|
||||||
|
|
||||||
|
|
||||||
|
elif player_choice == 'Rock':
|
||||||
|
print('You lost, you chose rock and the computer chose paper!')
|
||||||
|
losses += 1
|
||||||
|
rematch = input('Would you like to play another game? (Y/N)\n>')
|
||||||
|
if rematch == 'y' or rematch == 'Y':
|
||||||
|
continue
|
||||||
|
elif rematch == 'n' or rematch == 'N':
|
||||||
|
print(f'''OK, thanks for playing! The final score:
|
||||||
|
wins: {wins}, losses: {losses}, ties: {ties}''')
|
||||||
|
sys.exit()
|
||||||
|
|
||||||
|
|
||||||
|
elif computer_choice == 'Scissors':
|
||||||
|
if player_choice == 'Paper':
|
||||||
|
print('You lost, computer chose scissors and you chose paper!')
|
||||||
|
losses += 1
|
||||||
|
rematch = input('Would you like to play another game? (Y/N)\n>')
|
||||||
|
if rematch == 'y' or rematch == 'Y':
|
||||||
|
continue
|
||||||
|
elif rematch == 'n' or rematch == 'N':
|
||||||
|
print(f'''OK, thanks for playing! The final score:
|
||||||
|
wins: {wins}, losses: {losses}, ties: {ties}''')
|
||||||
|
sys.exit()
|
||||||
|
|
||||||
|
|
||||||
|
elif player_choice == 'Rock':
|
||||||
|
print('You won, computer chose scissors and you chose rock!')
|
||||||
|
wins += 1
|
||||||
|
rematch = input('Would you like to play another game? (Y/N)\n>')
|
||||||
|
if rematch == 'y' or rematch == 'Y':
|
||||||
|
continue
|
||||||
|
elif rematch == 'n' or rematch == 'N':
|
||||||
|
print(f'''OK, thanks for playing! The final score:
|
||||||
|
wins: {wins}, losses: {losses}, ties: {ties}''')
|
||||||
|
sys.exit()
|
||||||
|
|
||||||
|
|
||||||
@@ -1,66 +0,0 @@
|
|||||||
#############################################
|
|
||||||
# #
|
|
||||||
# Work in progress!! #
|
|
||||||
# #
|
|
||||||
#############################################
|
|
||||||
|
|
||||||
import random
|
|
||||||
import sys
|
|
||||||
import time
|
|
||||||
|
|
||||||
losses = 0
|
|
||||||
wins = 0
|
|
||||||
ties = 0
|
|
||||||
|
|
||||||
while True:
|
|
||||||
|
|
||||||
# Computer choice:
|
|
||||||
|
|
||||||
computer_choice = random.randint(1,3)
|
|
||||||
if computer_choice == 1:
|
|
||||||
computer_choice = 'Paper'
|
|
||||||
elif computer_choice == 2:
|
|
||||||
computer_choice = 'Rock'
|
|
||||||
elif computer_choice == 3:
|
|
||||||
computer_choice = 'Scissors'
|
|
||||||
|
|
||||||
# Player choice:
|
|
||||||
player_choice = input('What do you choose? Rock(R), Paper(P), or Scissors(S)?\n>')
|
|
||||||
if player_choice == 'P':
|
|
||||||
player_choice = 'Paper'
|
|
||||||
elif player_choice == 'R':
|
|
||||||
player_choice = 'Rock'
|
|
||||||
elif player_choice == 'S':
|
|
||||||
player_choice = 'Scissors'
|
|
||||||
else:
|
|
||||||
print(f'The choice you made ("{player_choice}") is not valid. The program will now exit')
|
|
||||||
sys.exit()
|
|
||||||
|
|
||||||
# Playing the game:
|
|
||||||
|
|
||||||
if computer_choice == player_choice:
|
|
||||||
print('It is a tie!')
|
|
||||||
ties += 1
|
|
||||||
elif computer_choice == 'Rock':
|
|
||||||
if player_choice == 'Scissors':
|
|
||||||
print('You lost, you chose Scissors and the computer chose rock!')
|
|
||||||
losses += 1
|
|
||||||
elif player_choice == 'Paper':
|
|
||||||
print('You won, you chose scissors and the computer chose rock!')
|
|
||||||
wins += 1
|
|
||||||
elif computer_choice == 'Paper':
|
|
||||||
if player_choice == 'Scissors':
|
|
||||||
print('You won, you chose Scissors and the computer chose paper!')
|
|
||||||
wins += 1
|
|
||||||
elif player_choice == 'Rock':
|
|
||||||
print('You lost, you chose rock and the computer chose paper!')
|
|
||||||
losses += 1
|
|
||||||
elif computer_choice == 'Scissors':
|
|
||||||
if player_choice == 'Paper':
|
|
||||||
print('You lost, computer chose scissors and you chose paper!')
|
|
||||||
losses += 1
|
|
||||||
elif player_choice == 'Rock':
|
|
||||||
print('You won, computer chose scissors and you chose rock!')
|
|
||||||
wins += 1
|
|
||||||
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user