Python challenge

[045~051] while 루프

Grace Ryu 2023. 9. 24. 10:31

 

#045. total이라는 변수를 0으로 설정한다. 
#total의 값이 50 이하면 사용자에게 숫자를 입력하라고 요청한다. 
#입력된 숫자를 total에 더하고 "the total is ..[total]"이라는 메세지를 출력한다. 
#total의 값이 50을 넘으면 루프를 멈추는 프로그램을 작성하라. 
total = 0
while total <= 50:
    num = int(input("숫자를 입력하세요: "))
    total = total + num 
    print("the total is", total)


#046. 사용자에게 숫자를 입력하라고 요청한다. 
#입력한 값이 5를 넘을때까지 숫자를 입력하라고 요청하고, 
#5를 넘으면 "the last number you entered was a [숫자]"를 출력하고 프로그램을 종료하라. 

num = 0 
while num <= 5:
    num = int(input("숫자를 입력하시오: "))
print(f"the last number you entered was {'kill', num}")
#print(f"the last number you entered was [{num}]")




#047. 사용자에게 숫자를 입력하라고 요청한 다음에, 다른 숫자를 입력하라고 하자.
#두 숫자를 더한 뒤, 또 다른 숫자를 더하고 싶은지 묻고 
#'y'라고 입력하면 다른숫자를 입력받아 더하고 다시 같은 질문을 한다. 
#'y'가 아닌 답을 하면 루프를 종료하고 총합을 출력하라

num1 = int(input("숫자를 입력하세요: "))
answer = 'y'

while answer == 'y':
        num2 = int(input("다른 숫자를 입력하세요: "))
        num1 = num1 + num2
        answer = str(input("다른 숫자를 더하고 싶습니까? y or n: "))
print("the total is", num1)



#048. 사용자가 파티에 초대하고 싶은 사람의 이름을 입력하라고 요청한다. 
#그 다음에 "[이름] has now been invited"라는 메세지를 출력하고 카운트에 1을 더한다
#다른 사람을 더 초대하고 싶은지 묻고 더이상 파티에 초대하고 싶은 사람이 없을때까지 반복한다. 
#초대하고 싶은 사람이 없다면 몇명 파티에 참석하는지를 표시하라. 

invite = 'y'
count = 0
while invite == 'y':
    name = str(input("파티에 초대하고 싶은 사람 이름을 쓰시오: "))
    print(f"{name} has now been invited")
    count = count + 1 
    invite = input("다른 사람을 더 초대하고 싶으신가요?: y or n: ")
print("파티에 참석하는 사람 수 :", count)


#049. compnum이라는 이름의 변수를 생성하고 50을 설정한다.
#사용자에게 숫자를 입력하라고 요청하고, 입력한 값이 compnum과 동일하지 않다면 
#입력한 값이 높은지 낮은지를 알려주고 다시 맞춰보라고 묻는다. 
#만약 compnum의 값과 일치하면 "well done, you took [카운트] attempts"라는 메세지 출력


compnum = 50
question = int(input("숫자를 입력하세요: "))
count = 0
while question != compnum:
    if question > compnum:
        print("too high")
    else:
        print("too low")
    count = count + 1
    question = int(input("다시 맞춰보세요: "))
print(f"well done, you took {count} attempts")



#050. 사용자에게 10과 20사이의 숫자를 입력하라고 요청한다.
#입력한 숫자가 10이하면 too low라고 메세지 출력하고 다시 입력하라고 요청 
#20 이상이면 "too high"라는 메세지 출력. 다시 입력하라고 요청 
#사용자가 10과 20사이의 값 입력할때까지 이 과정 반복
#10과 20사이 값 입력하면 thank you 메세지 출력

num = int(input("10~20사이 숫자 입력: "))

while num < 10 or num > 20 :
    if num < 10:
        print("Too low")
    else:
        print("Too high")
    num = int(input("숫자 다시 입력해 주세요: "))

print(input("thank you"))



#051. "10 green bottles" 노래를 이용
#"there are [숫자] green bottles hanging on the wall, [숫자] green bottles hanging on the wall, and if 1 green bottle should accidentally fall" 이라는 가사 출력
#"how many green bottles will be hanging on the wall?"이라고 질문
#만약 사용자가 맞히면 there will be [숫자] green bottles hanging on the wall 이라는 메세지 출력
#틀리면 no, try again 메세지 출력후 맞출때까지 반복
#green bottle 개수 0되면 "there are no more green bottles hanging on the wall"메세지 출력 종료


total = 0
while total <= 50 :
    num = int(input("숫자를 입력하세요: "))
    total = total + num

print("The total is...", total)


total = 0
while total <= 50:
    num = int(input("숫자를 입력하세요: "))
    total = total + num 
    print("the total is", total)