Adding edx work
This commit is contained in:
parent
c5814aaa1b
commit
041e82774f
7
edx-python/w1/1.py
Normal file
7
edx-python/w1/1.py
Normal file
|
@ -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
|
6
edx-python/w1/2.py
Normal file
6
edx-python/w1/2.py
Normal file
|
@ -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))
|
7
edx-python/w1/3.py
Normal file
7
edx-python/w1/3.py
Normal file
|
@ -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
|
8
edx-python/w1/4.py
Normal file
8
edx-python/w1/4.py
Normal file
|
@ -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))
|
7
edx-python/w1/5.py
Normal file
7
edx-python/w1/5.py
Normal file
|
@ -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))
|
5
edx-python/w1/6.py
Normal file
5
edx-python/w1/6.py
Normal file
|
@ -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))
|
16
edx-python/w1/problem-w1.py
Normal file
16
edx-python/w1/problem-w1.py
Normal file
|
@ -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)
|
7
edx-python/w1/problem3-w1.py
Normal file
7
edx-python/w1/problem3-w1.py
Normal file
|
@ -0,0 +1,7 @@
|
|||
import string
|
||||
|
||||
alphabet = string.ascii_lowercase
|
||||
|
||||
s = 'azcbobobegghakl'
|
||||
count = 0
|
||||
|
15
edx-python/w2/2.py
Normal file
15
edx-python/w2/2.py
Normal file
|
@ -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))
|
38
edx-python/w2/3.py
Normal file
38
edx-python/w2/3.py
Normal file
|
@ -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)
|
Loading…
Reference in a new issue