Ken Schaefer
8 months ago
3 changed files with 98 additions and 6 deletions
@ -0,0 +1,79 @@
|
||||
import random |
||||
|
||||
# Hello World |
||||
|
||||
print ('Hello World!') |
||||
print ('What is your name?') |
||||
myName = input() |
||||
print ('It is good to meet you ' + myName) |
||||
print ('The length of your name is: ') |
||||
print (len(myName)); |
||||
|
||||
# Conditional |
||||
|
||||
if myName == 'Ken': |
||||
print('Hello') |
||||
elif myName == 'Randolph': |
||||
print('Ftaghn') |
||||
else: |
||||
print('Hi') |
||||
|
||||
# Looping |
||||
|
||||
spam = 0 |
||||
while spam < 5: |
||||
print('Spam ') |
||||
spam = spam + 1 |
||||
|
||||
# Range |
||||
|
||||
total = 0 |
||||
for num in range(101): |
||||
total = total + num |
||||
print(total) |
||||
|
||||
for i in range(5): |
||||
print(random.randint(1, 10)) |
||||
|
||||
# Functions |
||||
|
||||
def hello(name): |
||||
print('Hello ' + name) |
||||
return 1 |
||||
|
||||
r = hello('Fred') |
||||
print(r) |
||||
|
||||
# Exceptions |
||||
|
||||
def spam(divideBy): |
||||
try: |
||||
return 42 / divideBy |
||||
except ZeroDivisionError: |
||||
print('Cant divide by 0') |
||||
|
||||
# Lists |
||||
|
||||
animals = ['cat', 'bat', 'rat', 'elephant'] |
||||
print(animals[1]) |
||||
print(animals[-1]) |
||||
|
||||
# Slices |
||||
|
||||
atimals = animals[1:3] |
||||
print(len(atimals)) |
||||
|
||||
animals.append('moose') |
||||
animals.remove('rat') |
||||
animals.sort() |
||||
print(animals) |
||||
|
||||
# Dictionaries |
||||
|
||||
aMonster = {'size':'large', 'color':'red'} |
||||
print(aMonster['size']) |
||||
|
||||
for v in aMonster.values(): |
||||
print(v) |
||||
|
||||
|
@ -1,6 +0,0 @@
|
||||
print ('Hello World!') |
||||
print ('What is your name?') |
||||
myName = input() |
||||
print ('It is good to meet you ' + myName) |
||||
print ('The length of your name is: ') |
||||
print (len(myName)); |
Loading…
Reference in new issue