n = input("Number? ") if n < 0: print "The absolute value of",n,"is",-n else: print "The absolute value of",n,"is",n
¿©±â¿¡ ³»°¡ ÀÌ ÇÁ·Î±×·¥À» µÎ ¹ø ½ÇÇàÇÑ Ãâ·Â°á°ú¸¦ º¸ÀδÙ:
Number? -34 The absolute value of -34 is 34 Number? 1 The absolute value of 1 is 1
ÀÚ, ÄÄÇ»ÅÍ´Â ÀÌ ÄÚµåÁ¶°¢À» º¸°í¼ ¹«¾ùÀ» Çϴ°¡? ¸ÕÀú ÄÄÇ»ÅÍ´Â »ç¿ëÀÚ¿¡°Ô ¼ýÀÚ Çϳª¸¦ "n = input("Number? ")"»ç¿ëÇÏ¿© ¿ä±¸ÇÑ´Ù. ´ÙÀ½À¸·Î ÄÄÇ»ÅÍ´Â "if n < 0:" ¶óÀÎÀ» Àаí¼
¸¸¾à n ÀÌ 0 º¸´Ù ÀÛ´Ù¸é ÆÄÀ̽ãÀº "print "The absolute value of",n,"is",-n"¶óÀÎÀ» ½ÇÇàÇÏ°í
±×·¸Áö ¾ÊÀ¸¸é "print "The absolute value of",n,"is",n"¶óÀÎÀ» ½ÇÇàÇÑ´Ù.
´õ¿í Çü½ÄÀûÀ¸·Î ÆÄÀ̽ãÀº Ç¥Çö½Ä n < 0 ÀÌ ÂüÀΰ¡ °ÅÁþÀΰ¡¸¦ »ìÇÉ´Ù. if ¼¼ú¹® ´ÙÀ½¿¡´Â ¼¼ú¹®ÀÇ ºí·ÏÀÌ µû¸¥´Ù. ¼¼ú¹®ÀÌ ÂüÀÏ °æ¿ì¿¡ ±× ºí·ÏÀº ½ÇÇàµÈ´Ù. ¼±ÅÃÀûÀ¸·Î if ¼¼ú¹® µÚ¿¡´Â else ¼¼ú¹®ÀÌ ÀÖÀ» ¼ö ÀÖ´Ù. else ¼¼ú¹®Àº ±× Ç¥Çö½ÄÀÌ °ÅÁþÀÏ °æ¿ì¿¡ ½ÇÇàµÈ´Ù.
ÇϳªÀÇ Ç¥Çö½ÄÀÌ °¡Áú¼ö ÀÖ´Â ¾à°£ÀÇ ´Ù¸¥ ¿¹µéÀÌ ÀÖ´Ù. ¿©±â¿¡ ±×µé ¸ðµÎ¸¦ ±â¼úÇÑ Ç¥¸¦ º¸ÀδÙ.:
¿¬»êÀÚ | ÇÔ¼ö |
< |
¹Ì¸¸ |
<= |
ÀÌÇÏ |
> |
ÃÊ°ú |
>= |
ÀÌ»ó |
== |
°°´Ù |
!= |
°°Áö ¾Ê´Ù |
<> |
not equalÀÇ ´Ù¸¥ Ç¥Çö |
if ¸í·É¾îÀÇ ¶Ç ´Ù¸¥ »ç¾çÀº elif ÀÌ´Ù. elif else if ÀÇ ¾àÀÚÀ̸ç
¿ø·¡ÀÇ if ¼¼ú¹®ÀÌ °ÅÁþÀÌ°í elif ºÎºÐÀÌ ÂüÀ̶ó¸é ±× ºÎºÐÀ» ½ÇÇàÇ϶ó´Â ¶æÀÌ´Ù.
¿©±â¿¡ ÇϳªÀÇ ¿¹Á¦°¡ ÀÖ´Ù:
a = 0 while a < 10: a = a + 1 if a > 5: print a," > ",5 elif a <= 7: print a," <= ",7 else: print "Neither test was true"
±×¸®°í ±× Ãâ·ÂÀº:
1 <= 7 2 <= 7 3 <= 7 4 <= 7 5 <= 7 6 > 5 7 > 5 8 > 5 9 > 5 10 > 5
if¹®ÀÌ ÂüÀÌ µÇÁö ¸øÇÒ ¶§¿¡¸¸ elif a <= 7 °¡ Æò°¡µÊÀ» ÁÖ¸ñÇ϶ó . elif ·Î ÇÑ°³ÀÇ if ¼¼ú¹®¾È¿¡¼ ´ÙÁß Å×½º¸¦ ÇÒ ¼ö ÀÖ´Ù.
High_low.py
#Plays the guessing game higher or lower # (originally written by Josh Cogliati, improved by Quique) #This should actually something that is semi random like the # last digits of the time or something else, but that will have to # wait till a later chapter. (Extra Credit, modify it to be random # after the Modules chapter) number = 78 guess = 0 while guess != number : guess = input ("Guess a number: ") if guess > number : print "Too high" elif guess < number : print "Too low" print "Just right"
Sample run:
Guess a number:100 Too high Guess a number:50 Too low Guess a number:75 Too low Guess a number:87 Too high Guess a number:81 Too high Guess a number:78 Just right