Python/Python Crash Course

Python Crash Course: 챕터 7

1june 2024. 6. 29. 18:26

Python Crash Course, 3rd Edition

 

  • Input() function
  • The input() function takes one argument: the prompt that
    we want to display to the user, so they know what kind of
    information to enter
  •  
  • Use CTRL + '(backtick) to switch from editor pane to terminal pane
prompt = "If you share your name, we can personalize the mess
ages you see."
prompt += "\nWhat is your first name? "
name = input(prompt)
print(f"\nHello, {name}!")

Multiline string for input() function is achievable using +- operator, see above

Notice how there must always be a space at the end of the string of the input for clarity

 

  • int() function
height = input("How tall are you, in inches? ")
height = int(height)

if height >= 48:
	print("\nYou're tall enough to ride!")
else:
	print("\nYou'll be able to ride when you're a little older.")

int() function converts the input string into a numerical value

  • Modulo operator %
number = input("Enter a number, and I'll tell you if it's even or odd: ")
number = int(number)

if number % 2 == 0:
	print(f"\nThe number {number} is even.")
else:
	print(f"\nThe number {number} is odd.")

It only tells you the remainder, it doesn't tell you how many times one number fits into another number.

 

 

  • While loop
prompt = "\nTell me something, and I will repeat it back to you:"
prompt += "\nEnter 'quit' to end the program. "
message = ""

while message != 'quit':
    message = input(prompt)
    print(message)

print("Program ended.")

While loop keeps on looping until the condition defined is met.

Notice how there's a variable with empty string before using While loop

 

  • Flag
  • A flag is a variable that is used to act as a signal in the program.  
prompt = "\nTell me something, and I will repeat it back to y
ou:"
prompt += "\nEnter 'quit' to end the program. "
active = True
while active:
	message = input(prompt)
	if message == 'quit':
		active = False
	else:
		print(message)

Now that we have a flag to indicate
whether the overall program is in an active state, it would
be easy to add more tests (such as elif statements) for
events that should cause active to become False

 

  • Break statement
  • break statement is used to exit any loop immediately
prompt = "\nPlease enter the name of a city you have visite
d:"
prompt += "\n(Enter 'quit' when you are finished.) "
while True:
	city = input(prompt)
	if city == 'quit':
		break
	else:
		print(f"I'd love to go to {city.title()}!")

 

A loop that starts with while True will run forever unless it reaches a break statement

 

  • Continue statement
  • continue statement is used to return to the beginning of the loop, instead of breaking out of loop like break statement; ignoring the rest of the codelines
  •  
current_number = 0
while current_number < 10:
	current_number += 1
	if current_number % 2 == 0:
		continue
	print(current_number)

It gives out all the odd numbers as ouput

*To cancel infinite loop mistake, press CTRL-C

'Python > Python Crash Course' 카테고리의 다른 글

Python Crash Course: 챕터 9  (0) 2024.07.01
Python Crash Course: 챕터 8  (1) 2024.07.01
Python Crash Course: 챕터 6  (0) 2024.06.28
Python Crash Course: 챕터 5  (0) 2024.06.27
Python Crash Course: 챕터 4  (0) 2024.06.26