From 041e82774f2e2b55d84c18125e2eec38e48e2d3e Mon Sep 17 00:00:00 2001 From: Christopher Talib Date: Wed, 3 Jul 2019 12:19:44 +0200 Subject: [PATCH] Adding edx work --- edx-python/w1/1.py | 7 +++++++ edx-python/w1/2.py | 6 ++++++ edx-python/w1/3.py | 7 +++++++ edx-python/w1/4.py | 8 ++++++++ edx-python/w1/5.py | 7 +++++++ edx-python/w1/6.py | 5 +++++ edx-python/w1/problem-w1.py | 16 +++++++++++++++ edx-python/w1/problem3-w1.py | 7 +++++++ edx-python/w2/2.py | 15 ++++++++++++++ edx-python/w2/3.py | 38 ++++++++++++++++++++++++++++++++++++ 10 files changed, 116 insertions(+) create mode 100644 edx-python/w1/1.py create mode 100644 edx-python/w1/2.py create mode 100644 edx-python/w1/3.py create mode 100644 edx-python/w1/4.py create mode 100644 edx-python/w1/5.py create mode 100644 edx-python/w1/6.py create mode 100644 edx-python/w1/problem-w1.py create mode 100644 edx-python/w1/problem3-w1.py create mode 100644 edx-python/w2/2.py create mode 100644 edx-python/w2/3.py diff --git a/edx-python/w1/1.py b/edx-python/w1/1.py new file mode 100644 index 0000000..4cc352d --- /dev/null +++ b/edx-python/w1/1.py @@ -0,0 +1,7 @@ +iteration = 0 +count = 0 +while iteration < 5: + for letter in "hello, world": + count += 1 + print("Iteration " + str(iteration) + "; count is: " + str(count)) + iteration += 1 \ No newline at end of file diff --git a/edx-python/w1/2.py b/edx-python/w1/2.py new file mode 100644 index 0000000..011623d --- /dev/null +++ b/edx-python/w1/2.py @@ -0,0 +1,6 @@ +for iteration in range(5): + count = 0 + while True: + for letter in "hello, world": + count += 1 + print("Iteration " + str(iteration) + "; count is: " + str(count)) \ No newline at end of file diff --git a/edx-python/w1/3.py b/edx-python/w1/3.py new file mode 100644 index 0000000..72fcbb2 --- /dev/null +++ b/edx-python/w1/3.py @@ -0,0 +1,7 @@ +for iteration in range(5): + count = 0 + while True: + for letter in "hello, world": + count += 1 + print("Iteration " + str(iteration) + "; count is: " + str(count)) + break diff --git a/edx-python/w1/4.py b/edx-python/w1/4.py new file mode 100644 index 0000000..1c78394 --- /dev/null +++ b/edx-python/w1/4.py @@ -0,0 +1,8 @@ +count = 0 +phrase = "hello, world" +for iteration in range(5): + index = 0 + while index < len(phrase): + count += 1 + index += 1 + print("Iteration " + str(iteration) + "; count is: " + str(count)) diff --git a/edx-python/w1/5.py b/edx-python/w1/5.py new file mode 100644 index 0000000..08d64f6 --- /dev/null +++ b/edx-python/w1/5.py @@ -0,0 +1,7 @@ +count = 0 +phrase = "hello, world" +for iteration in range(5): + while True: + count += len(phrase) + break + print("Iteration " + str(iteration) + "; count is: " + str(count)) diff --git a/edx-python/w1/6.py b/edx-python/w1/6.py new file mode 100644 index 0000000..5ddd0ab --- /dev/null +++ b/edx-python/w1/6.py @@ -0,0 +1,5 @@ +count = 0 +phrase = "hello, world" +for iteration in range(5): + count += len(phrase) + print("Iteration " + str(iteration) + "; count is: " + str(count)) diff --git a/edx-python/w1/problem-w1.py b/edx-python/w1/problem-w1.py new file mode 100644 index 0000000..b01ed8a --- /dev/null +++ b/edx-python/w1/problem-w1.py @@ -0,0 +1,16 @@ +# Assume s is a string of lower case characters. + +# Write a program that counts up the number of vowels contained in the string s. Valid vowels are: 'a', 'e', 'i', 'o', and 'u'. For example, if s = 'azcbobobegghakl', your program should print: + +# Number of vowels: 5 +s = 'obbbobobkobobonboobldhbobboobybobbobob' + + +bob = 'bob' +count = 0 + +for i in range(len(s)): + if s.startswith('bob', i): + count += 1 + +print("Number of bobs:", count) \ No newline at end of file diff --git a/edx-python/w1/problem3-w1.py b/edx-python/w1/problem3-w1.py new file mode 100644 index 0000000..7fa0217 --- /dev/null +++ b/edx-python/w1/problem3-w1.py @@ -0,0 +1,7 @@ +import string + +alphabet = string.ascii_lowercase + +s = 'azcbobobegghakl' +count = 0 + diff --git a/edx-python/w2/2.py b/edx-python/w2/2.py new file mode 100644 index 0000000..bc32c05 --- /dev/null +++ b/edx-python/w2/2.py @@ -0,0 +1,15 @@ +x = 23 +epsilon = 0.01 +step = 0.1 +guess = 0.0 + +while abs(guess**2-x) >= epsilon: + if guess <= x: + guess += step + else: + break + +if abs(guess**2 - x) >= epsilon: + print('failed') +else: + print('succeeded: ' + str(guess)) \ No newline at end of file diff --git a/edx-python/w2/3.py b/edx-python/w2/3.py new file mode 100644 index 0000000..c9b99af --- /dev/null +++ b/edx-python/w2/3.py @@ -0,0 +1,38 @@ +print("Please think of a number between 0 and 100!") +guess = input() + +computer_guess = 50 +user_input = "" +correct_input = False + +def checkUserInput(user_input): + if user_input == 'l' or user_input == 'h' or user_input == 'c': + return True + else: + print("Sorry, I did not understand your input.") + return False + +def checkValue(user_input, guess): + if user_input == 'h': + new_guess = guess//2 + return new_guess + elif user_input == 'l': + new_guess = guess + guess//2 + return new_guess + elif user_input == 'c': + return guess + else: + raise ValueError + +while guess != computer_guess: + print("Is your secret number", computer_guess, "?") + print("Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly.") + user_input = input() + if correct_input == False: + correct_input = checkUserInput(user_input) + computer_guess = checkValue(user_input, computer_guess) + else: + computer_guess = checkValue(user_input, computer_guess) + + +print("Game over. Your secret number was:", computer_guess)