Complete-Python-3-Bootcamp_04-Milestone Project - 1_00-Warm-Up-Project-Exercises.ipynb at master ·
Complete-Python-3-Bootcamp_04-Milestone Project - 1_00-Warm-Up-Project-Exercises.ipynb at master ·
master
1 contributor
___
In [3]:
def display_list(mylist):
print(mylist)
In [4]:
mylist = [0,1,2,3,4,5,6,7,8,9,10]
display_list(mylist)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
In [8]:
input('Please enter a value: ')
In [10]:
result = input("Please enter a number: ")
In [11]:
result
Out[11]: '2'
In [12]:
type(result)
Out[12]: str
In [13]:
int(result)
Out[13]: 2
In [15]:
result = int(input("Please enter a number: "))
In [17]:
type(result)
Out[17]: int
In [19]:
# Example of an error!
result = int(input("Please enter a number: "))
In [20]:
def user_choice():
'''
User inputs a number (0-10) and we return this in integer form.
No parameter is passed when calling this function.
'''
choice = input("Please input a number (0-10)")
return int(choice)
In [21]:
user_choice()
In [22]:
result = user_choice()
In [23]:
result
Out[23]: 2
In [24]:
type(result)
Out[24]: int
As you get more advanced, you can start looking at other ways of doing this (these
methods will make more sense later on in the course, so don't worry about them for
now).
In [31]:
some_input = '10'
In [32]:
# Lot's of .is methods availble on string
some_input.isdigit()
Out[32]: True
In [35]:
def user_choice():
# We can convert once the while loop above has confirmed we have a digit
return int(choice)
In [38]:
user_choice()
In [39]:
def user_choice():
# We can convert once the while loop above has confirmed we have a digit
return int(choice)
In [40]:
user_choice()
Now let's explore how to "clear" the output, that way we don't see the history
of the "Choose a number" statements.
NOTE: Jupyter Notebook users will use the IPython method shown here. Other
IDE users (PyCharm, VS, etc..) will use
In [3]:
from IPython.display import clear_output
clear_output()
In [4]:
def user_choice():
if choice.isdigit() == False:
# THIS CLEARS THE CURRENT OUTPUT BELOW THE CELL
clear_output()
print("Sorry, but you did not enter an integer. Please try again
# We can convert once the while loop above has confirmed we have a digit
return int(choice)
In [5]:
user_choice()
Choose a number: 2
Out[5]: 2
In [1]:
result = 'wrong value'
acceptable_values = ['0','1','2']
In [2]:
result in acceptable_values
Out[2]: False
In [7]:
result not in acceptable_values
Out[7]: True
In [16]:
from IPython.display import clear_output
clear_output()
In [11]:
def user_choice():
print("Sorry, but you did not choose a value in the correct rang
# We can convert once the while loop above has confirmed we have a digit
return int(choice)
In [12]:
user_choice()
choice ='WRONG'
within_range = False
if choice.isdigit() == False:
print("Sorry that is not a digit!")
if choice.isdigit() == True:
if int(choice) in range(0,10):
within_range = True
else:
within_range = False
return int(choice)
In [2]:
user_choice()
In [2]:
game_list = [0,1,2]
In [10]:
def display_game(game_list):
print("Here is the current list")
print(game_list)
In [11]:
display_game(game_list)
In [12]:
def position_choice():
# We can convert once the while loop above has confirmed we have a digit
return int(choice)
In [13]:
def replacement_choice(game_list,position):
game_list[position] = user_placement
return game_list
In [14]:
def gameon choice():
https://github.com/Pierian-Data/Complete-Python-3-Bootcamp/blob/master/04-Milestone Project - 1/00-Warm-Up-Project-Exercises.ipynb 8/9
6/30/23, 9:24 AM Complete-Python-3-Bootcamp/04-Milestone Project - 1/00-Warm-Up-Project-Exercises.ipynb at master · Pierian-Data/Complete-P…
de ga eo _c o ce():
if choice == "Y":
# Game is still on
return True
else:
# Game is over
return False
In [18]:
# Variable to keep game playing
game_on = True
while game_on: