๐Ÿ‘ฉ‍๐Ÿ’ป/ruby & python

[python & ruby] ์ž…๋ ฅ๊ณผ ์ถœ๋ ฅ

ํ•œ๋‚˜ 2020. 3. 17. 19:46

์ƒํ™œ์ฝ”๋”ฉ์œผ๋กœ ํ•™์Šต ์ค‘. 

 

1) ๋ฃจ๋น„์—์„œ ์‚ฌ์šฉ์ž์˜ ์ž…๋ ฅ์„ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์œผ๋กœ ๊ฐ€์ง€๊ณ  ์˜ค๊ธฐ

 

puts("์ž…๋ ฅํ•ด์ฃผ์„ธ์š”")
in_str = gets.chomp()
puts(in_str.capitalize() + " world!")

 

์ž…๋ ฅํ•ด์ฃผ์„ธ์š”๋ฅผ ํ™”๋ฉด์— ์ถœ๋ ฅํ•˜๊ณ , gets.chomp()๋ฅผ ์ด์šฉํ•ด ์‚ฌ์šฉ์ž์˜ ์ž…๋ ฅ๊ฐ’์„ ๋ฐ›์•„์™€ ๋ณ€์ˆ˜ in_str์— ์ €์žฅ. ๋ฐ›์•„์˜จ ๋ณ€์ˆ˜๋ฅผ '๋ฌธ์ž์—ด์˜ ์ฒซ ๊ธ€์ž๋ฅผ ๋Œ€๋ฌธ์ž๋กœ ๋ฐ”๊ฟ”์ฃผ๋Š”' capitalize()๋ฅผ ์ ์šฉํ•ด ๋’ค์— ์ƒˆ๋กœ ์ž…๋ ฅํ•œ world!์™€ ํ•จ๊ป˜ ์ถœ๋ ฅ๋˜๋„๋ก ํ•˜๊ธฐ. 

 

๊ฒฐ๊ณผ๋Š” ์•„๋ž˜์ฒ˜๋Ÿผ hannah๋กœ ์ž…๋ ฅํ•œ ๋ฌธ์ž์—ด์ด Hannah world!๋กœ ์ถœ๋ ฅ๋œ ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. 

 

 

2) ํŒŒ์ด์ฌ์—์„œ ์‚ฌ์šฉ์ž์˜ ์ž…๋ ฅ์„ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์œผ๋กœ ๊ฐ€์ง€๊ณ  ์˜ค๊ธฐ

 

in_str = input("์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.\n")
print(in_str.upper()+" World!")

 

ํŒŒ์ด์ฌ์—์„œ๋Š” ์กฐ๊ธˆ ๋” ๊ฐ„๋žตํ•œ ์ฝ”๋“œ๋กœ ๊ฐ€๋Šฅํ•˜๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” .upper()๋กœ, ๋ชจ๋“  ๋ฌธ์ž์—ด์„ ๋Œ€๋ฌธ์ž๋กœ ๋งŒ๋“ค์–ด์„œ world!์™€ ํ•จ๊ป˜ ์ถœ๋ ฅํ•œ๋‹ค. 

 

 

3) ๋ฃจ๋น„์—์„œ ๋กœ๊ทธ์ธ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์— ์ž…๋ ฅ ๊ธฐ๋Šฅ ์ถ”๊ฐ€ํ•˜๊ธฐ

 

puts("์•„์ด๋””๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”")
input = gets.chomp()
real_hannah = "11"
real_jessy = "ab"

if real_hannah == input
  puts("Hello!, hannah")
elsif real_jessy == input
  puts("Hello!, jessy")
else
  puts("who are you?")
end

์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ–ˆ์„ ๋•Œ 11๊ณผ ๊ฐ™๋‹ค๋ฉด hannah๋ฅผ ํ™˜์˜ํ•˜๋Š” ๋ฉ”์„ธ์ง€๊ฐ€ ๋œจ๊ณ , ab๋ฅผ ์ž…๋ ฅํ–ˆ์„ ๋•Œ๋Š” jessy๋ฅผ ํ™˜์˜ํ•˜๋Š” ๋ฌธ๊ตฌ๊ฐ€ ๋œฌ๋‹ค. 

4) ํŒŒ์ด์ฌ์—์„œ ๋กœ๊ทธ์ธ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ž…๋ ฅ ๊ธฐ๋Šฅ ์ถ”๊ฐ€ํ•˜๊ธฐ

 

note : ์‚ฌ์šฉ์ž ์ž…๋ ฅ๊ฐ’์„ ๋ฐ›๊ธฐ ์œ„ํ•ด input์„ ์‹คํ–‰์‹œ์ผฐ์„ ๋•Œ ์ˆซ์ž 11์„ ์ž…๋ ฅํ•œ ๊ฒƒ์€ ์ˆซ์ž๊ฐ€ ์•„๋‹Œ string, ๋ฌธ์ž ๋ฐ์ดํ„ฐํ˜•์œผ๋กœ ์—ฌ๊ฒจ์ง„๋‹ค. 

 

in_str = input("์•„์ด๋””๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.\n")
print(type(in_str))

 

 

in_str = input("์•„์ด๋””๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.\n")

hannah = 11
if in_str == hannah:
    print("correct")
else:
    print("not correct")

hannah ๋ผ๋Š” ๋ณ€์ˆ˜๊ฐ€ ์ˆซ์ž์ผ ๋•Œ๋Š” not correct๊ฐ€ ์ถœ๋ ฅ๋˜๋ฉฐ, hannah = "11" ์ผ ๋•Œ๋Š” correct๊ฐ€ ์ถœ๋ ฅ๋จ.

 

in_str = input("์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.\n")
real_hannah = "11"
real_jessy = "ab"

if real_hannah == in_str:
    print("Hello!, hannah")
elif real_jessy == in_str:
    print("Hello!, jessy")
else:
    print("Who are you?")

 

์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ–ˆ์„ ๋•Œ 11๊ณผ ๊ฐ™๋‹ค๋ฉด hannah๋ฅผ ํ™˜์˜ํ•˜๋Š” ๋ฉ”์„ธ์ง€๊ฐ€ ๋œจ๊ณ , ab๋ฅผ ์ž…๋ ฅํ–ˆ์„ ๋•Œ๋Š” jessy๋ฅผ ํ™˜์˜ํ•˜๋Š” ๋ฌธ๊ตฌ๊ฐ€ ๋œฌ๋‹ค.