|
Variable
|
Hold a value, can be change
|
|
string
|
A list of number/letter/symbols
|
|
Float number
|
Whole number/counting number
|
|
integer number
|
The number in decimal
|
|
boolean
|
True/False
|
|
Modulo
|
Fine the remainder
|
|
syntax
|
Grammar/structure of lauguage
|
|
length
|
the length of the string
|
|
print()
|
Show information that you want on the screen
|
|
input()
|
Gain information from user
|
|
Float()
|
Change to the decimal number
|
|
int()
|
change to the number integer
|
|
str()
|
A list of number/letter/symbols
|
|
len()
|
The length of the string
|
|
#
|
To note, no effect
|
|
""
|
Multi-line comment
|
|
If..... :then..... else.......
|
If the statement is true then do command under then else do command under else
|
|
while......
|
While this is true loop the command under the conditional
|
|
While True
|
loops forever
|
|
for each item in name of list
|
For every item in the list repeat the command under the loop that many times. (a string is a list too)
|
import random
mylist = ['lion', 'cheetah', 'panther', 'cougar', 'leopard']
random_item = random.choice(mylist)
Chances = 5
Score = 0
while Chances > 0:
print("Words:['lion', 'cheetah', 'panther', 'cougar', 'leopard']")
user_guess = input("Guess a word: ")
if user_guess == random_item:
print("That's correct!")
Score = Score+100
print("Chances remaining",'',Chances)
random_item = random.choice(mylist)
print("Score is",'',Score)
else:
if user_guess in mylist:
print("Sorry, wrong choice!")
Chances = Chances - 1
print("Chances remaining",'',Chances)
print("Score is",'',Score)
else:
print("Sorry, that is not even in the list!")
Chances = Chances - 1
print("Chances remaining",'',Chances)
print("Score is",'', Score)
if Chances == 0:
print("Gameover",'',"The word is",'',random_item)
print("Final score is",'',Score)
def printDefinitions(word):
#variable
if word == "variable":
print ("""
A varible is value that can be change
""")
#function
elif word == "function":
print ("""
A function is block of quote can be reused
""")
#parameter
elif word == "parameter":
print ("""
A parameter is value that inside the blacket of the function
""")
#argument
elif word == "argument":
print ("""
A argument is value that inside the blacket of the function
""")
# function call
elif word == "function call":
print ("""
A function call is something that make the function run
""")
#string
elif word == "string":
print ("""
A string is list of character
""")
else:
print("unknown word")
return
user_input=input("Enter word")
printDefinitions(user_input)
#write a function that returns the largest of two values
#name : max2
#agruments: num1, num2
# return: largest value
# write a functrion that returns the largest of three values
# name : max3
#agrument: num1, num2, num3
# return: largest value
def max2(num1,num2):
if num1 >= num2:
max_value = (num1)
if num2 > num1:
max_value = (num2)
return max_value
num1 = input('Enter the the first value')
num2 = input('Enter the the second value')
print (max2(num1,num2))
def max3(num1,num2,num3):
if num1 >= num2 and num1 >= num3:
max_value = (num1)
if num2 > num1 and num2 >= num3:
max_value = (num2)
if num3 >= num2 and num3 >= num1:
max_value = (num3)
return max_value
num3 = input('Enter the the third value')
print (max3(num1,num2,num3))
|
==
|
equal to
|
|
!=
|
not equal to
|
|
<
|
less than
|
|
<=
|
less than or equal to
|
|
>
|
greater than
|
|
>=
|
greater than or equal to
|
|
+
|
add
|
|
-
|
subtract
|
|
*
|
multiply
|
|
/
|
divide and quotient is float
|
|
//
|
divide and quotient is integer
|
|
**
|
exponent
|
|
%
|
modulo: the remainder
|
|
string * string
|
CRASH!
|
|
number * number
|
math (multiply)
|
|
string * number
|
combines the strings multiple time
|
|
string ** number
|
CRASH!
|
|
number ** number
|
exponent(Math)
|
|
string + string
|
squishes them together
|
|
string + number
|
CRASH!
|
|
number + number
|
math(addition)
|
|
True or anything =
|
True
|
|
False and anything =
|
False
|
|
range(5) =
|
[0,1,2,3,4]
|
|
print("hello", "there")
|
#displays hello there
|
|
print("hello" + "there")
|
#displays hellothere
|
|
"hi" + "there"
|
== "hithere"
|
|
"hi" * 5
|
== "hihihihihi"
|
|
while True:
|
# forever
|
thelist = [4, 3, 2, 1, 0]
index = 0 # start at the first item
while index < len(thelist):
print (thelist[index]) #prints each item
index = index + 1
forlist = [3, 4, 5, 2, 1]
for item in forlist:
print(item)
mystr = "hello123"
numbers = [1,2,3,4,5,6]
print (numbers)
shoppinglist = ['shoes','bags','pants','shirts']
print (shoppinglist)
mixed = [1, 'hello', 2.5, True, False]
print (mixed)
letter_num = 0
while letter_num < len(mystr):
print (mystr[letter_num])
letter_num = letter_num + 1
for myletterisawesome in mystr:
print(myletterisawesome)
for tientien in shoppinglist:
print(tiemtiem)
out = 0
for mrtim in shoppinglist:
out = out + 1
def myprintnew(text, decoration):
print(decoration + str(text) + decoration)
return
myprintnew(1, "+++")
myprintnew('hello', '-=-=-=-=-=-=-=-=')
myprintnew(1, "@@@@@@@")
def areaOfCircle (r):
if r <= 0:
return "Error: invalid radius"
pi = 3.1415
area = pi\*r\*\*2
return area
user_radius = float(input("Enter the radius: "))
print('The area of the circle is', areaOfCircle(user_radius))
def doubleit(number):
return number * 2
print (doubleit(3))
print (doubleit(doubleit(4)))
myvar = 12
myvar = doubleit(myvar)
myvar = doubleit(myvar)
print (myvar)
def printDefinitions(word):
#variable
if word == "variable":
print ("""
A varible is value that can be change
""")
#function
elif word == "function":
print ("""
A function is block of quote can be reused
""")
#parameter
elif word == "parameter":
print ("""
A parameter is value that inside the blacket of the function
""")
#argument
elif word == "argument":
print ("""
A argument is value that inside the blacket of the function
""")
# function call
elif word == "function call":
print ("""
A function call is something that make the function run
""")
#string
elif word == "string":
print ("""
A string is list of character
""")
else:
print("unknown word")
return
**while True:**
**user_input=input("Enter word")**
**printDefinitions(user_input)**
#write the function that returns the largest number in a list
#name: maxlist
#argument:list
#returns the largest value in the list
def maxlist(list):
maxvalue = list[0]
for item in list:
if item > maxvalue:
maxvalue = item
return maxvalue
mylist = [1,2,3,4,55,66,777,0,1]
print(maxlist(mylist))
while True :
user_word = input("Enter your word")
if user_word != ("quit"):
print(len(user_word))
if user_word == ("quit"):
break
reverse = ""
letter_num = 0
while letter_num < len(user_word):
reverse = user_word[letter_num] + reverse
letter_num = letter_num + 1
if user_word == reverse:
print (user_word, "is palindrome")
else:
print (user_word, "is not palindrome")
word = input("Type in an word: ")
reverse = ""
for letter in word:
reverse = letter + reverse
print ("Reverse: ", reverse)
print(True)
print (2<3)
print (2 != 2)
user_number = input("Please enter a number: ")
number = int(user_number)
countdown_string = ""
while number > 0:
countdown_string = countdown_string + " " + str(number)
number = number-1
print (countdown_string)
word = input("What is the word ?")
reverse = ""
"""
letter_num = 0
while letter_num < len(word):
reverse = word[letter_num] + reverse
letter_num = letter_num + 1
"""
for letter in word:
reverse = letter + reverse
print ("Reverse: ",reverse)
out = 0
for letter in word:
out = out + 1
print(out)
user_number = input("Please enter a number")
number = int(user_number)
binary_string =''
while (number > 0):
remainder= number%2
binary_string = str(remainder) + binary_string
number= number//2
print("Binary string is", binary_string)
import random
inlist = (1,2,3,4,5,6,7)
random_int = random.choice(inlist)
print (inlist, '', random_int)
fplist = (1.1,2.1,3.1,4.1,5.1,6.1,7.1)
random_fp = random.choice(fplist)
print (fplist, '', random_fp)
strlist = ('love','captain','verymuch')
random_str = random.choice(strlist)
print (strlist, '', random_str)
mylist = (1,2,3,4,5,6,7,1.1,2.1,3.1,4.1,5.1,6.1,7.1,'love','captain','verymuch')
random_item = random.choice(mylist)
print (mylist, '', random_item)
myvar1 = 1
myvar2 = 2
myvar3 = 3
varlist = (myvar1,myvar2,myvar3)
random_var = random.choice(varlist)
print (varlist, '', random_var)
reverse = ""
letter_num = 0
word = input('type in a word: ')
while letter_num < len(word):
reverse = word[letter_num] + reverse
letter_num = letter_num + 1
#create a function that will ask user for a string
#and then say if that string is palindrome or not
reverse = ""
letter_num = 0
word = input('type in a word: ')
while letter_num < len(word):
reverse = word[letter_num] + reverse
letter_num = letter_num + 1
if word == reverse:
print ("It is palindrome")
else:
print ("It is not palindrome")
#write a function that computes the area of triangle
#name: areOfTriangle
#parameter: b, h
#return: area
def areaOfTriangle(b, h):
if user_base <= 0:
return "Error: invarid radius"
if user_height <= 0:
return "Error: invarid radius"
area = b * h / 2
return area
user_base = float(input('Enter the base of the triangle: '))
user_height = float(input('Enter the height of the triangle: '))
print ('The area of triangle is', areaOfTriangle(user_base, user_height))
#write a function that computes the volume of a prism
#name: volumeOfPrism
#return: volume
def volumeOfPrism(b, h, l):
if user_length <= 0:
return "Error: invarid radius"
volume = b \* h \* l / 2
return volume
user_length = float(input('Enter the length of the prism: '))
print('The volume of the prism is',volumeOfPrism(user_base, user_height, user_length))
'''
Apisara Yooyuen Jaja 5861004, 1005
'''
def isPalindrome(word):
reverse = ""
letter_num = 0
while letter_num < len(user_word):
reverse = user_word[letter_num] + reverse
letter_num = letter_num + 1
if word == reverse:
return True
else:
return False
while True :
user_word = input("Enter your word: ")
word = len(user_word)
if user_word == ("quit"):
break
if isPalindrome(user_word):
print(word)
print (user_word, "is palindrome")
else:
print(word)
print (user_word, "is not palindrome")
No comments yet.
Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.