Python - Practice
This is some practice on string manipulation, for and while loops, and guess and check.
https://www.youtube.com/watch?v=SE4P7IVCunE
s = 'abc'print(len(s)) print(s[0]) print(s[1]) print(s[2]) print(s[-1]) print(s[-2]) print(s[-3]) t = 'hdfjlhdjlfh'print(t[::]) print(t[0:4]) h = 'hello'print(h) h = 'y' + h[1:len(h)] print(h) for var in range(5): print('this will print 5 times, starting with 0 and ending with 4') for var2 in range(1, 5): print('this will print 4 times, starting with 1 and ending with 4') for letter in t: if letter == 'h': print('this is an h!') else: print('this is not an h!') an_letters = 'abcdefghijklmnopqrstuvwxyz'word = input('Enter a word:') times = int(input('How many times?')) i = 0 while i < len(word): letter = word[i] if letter in an_letters: print('give me an ' + letter + '!' + letter) i += 1 print('what does it spell?') for i in range(times): print(word.upper(), '!!') an_letters = 'abcdefghijtuvwxyz'word = input('Enter a word:') times = int(input('How many times?')) for letter in word: if letter in an_letters: print('give me an ' + letter + '!' + letter) else: print('give me an ' + letter + '!' + letter) # notice that this time the else statement is in place ...# ...because the an_letters are not complete. print('what does it spell?') for i in range(times): print(word.upper(), '!!') cube = 27 for guess in range(cube+1): if guess**3 == cube: print('cube root of', cube, 'is', guess)
https://www.youtube.com/watch?v=SE4P7IVCunE
Comments