🤍 전 강의 25% 할인 중 🤍

2024년 상반기를 돌아보고 하반기에도 함께 성장해요!
인프런이 준비한 25% 할인 받으러 가기 >>

  • 카테고리

    질문 & 답변
  • 세부 분야

    프로그래밍 언어

  • 해결 여부

    미해결

while이나 for 또는 if 같은 구문 조건에

20.03.18 21:16 작성 조회수 120

0

숫자가 아니라 특정 문구를 넣으면 동작이 안되나요?

예를 들어 while x == "and" or  x == "ios"

로 x가 and이거나 ios일때까지 하고 싶은데

and를 입력해도 ios를 입력해도 반복되는건 어떻게 해야하나요?

답변 5

·

답변을 작성해보세요.

1

안녕하세요

while 조건을 작성하신 내용처럼 하려면 while 문 이전에 이미 x 라는 변수에 and 나 ios 라는 값이 들어가있어야 합니다. 그리고 그렇게 되면 원하시는대로 and 이거나 ios 일때까지.. 란 조건은 이미 이뤄진 상태이기 때문에 while 문이 의미가 없게 됩니다. 아마 이런 코드를 원하신게 아닌가 싶네요 ^^

while True:
    x = input("입력값 : ")
    if x == "and" or x == "ios":
        break

print("종료")

0

^^ 조금 뒤에 "예외 처리" 부분을 배우시면 더 좋은 방법을 배우게 될거에요 !

0

baemongs님의 프로필

baemongs

질문자

2020.03.18

print("Type your height in centimeter :",end=" ")
height = input()

while True:
    if str(0) < height < str(400):
        break
    else:
        height = input("You can type only in number. Type your height in centimeter : ")

이렇게 해결했습니다!!

0

baemongs님의 프로필

baemongs

질문자

2020.03.18

선생님~ 퀴즈#6을 코딩하고 있는데요. 좀 더 구체적인 코딩을 하고 있는 중입니다.

사용자 입력으로 키를 입력받는 중에 사용자가 int가 아닌 다른 type의 엉뚱한 입력값을 넣으면 다시 입력받을 수 있는 방법은 없을까요?

if type(height) != int:

 다시 입력하도록 하는 코드

이런 느낌을 만들고 싶은데 잘 모르겠네요..
def std_weight(heightgender):
    if gender == "남자":
        std = pow(int(height)/100,2)*22
    elif gender == "여자":
        std = pow(int(height)/100,2)*21
    return round(std,2)

# print("Type m if you are man, Type f if you are woman :",end=" ")
choice = input("Type m if you are man, Type f if you are woman : ")
while True:
    if choice == "m":
        gender = "남자"
        break
    elif choice == "f":
        gender = "여자"
        break
    else:
        print("You write wrong answer.")
        print("Type \"m\" or \"f\" only.")
        print("Type \"m\" if you are a man, type \"f\" if you are a woman :",end=" ")
        choice = input()

print("Type your height in centimeter :",end=" ")
height = input()

while True:
    if 0 < int(height) < 400:
        break
    else:
        print("You can type only in number. Type your height in centimeter :"end=" ")
        height = input()

print("키 {0}cm {1}의 표준 체중은 {2}kg 입니다.".format(height, gender, std_weight(height, gender)))
.
.
.
여기까지 제가 작성한 코드구요. 잘못된 값을 입력하는 것때문에 동작이 멈추고 싶지는 않습니다.
아래는 height에 int가 아닌 값을 입력하면 나는 오류코드입니다.
너무 어려운걸 도전하고 있나요?ㅠㅠ
.
.
Type m if you are man, Type f if you are woman : f
Type your height in centimeter : qqq
Traceback (most recent call last):
File "c:/Users/인영/Desktop/PythonWorkSpace/helloworld.py", line 27, in <module>
if 0 < int(height) < 400:
ValueError: invalid literal for int() with base 10: 'qqq'

0

baemongs님의 프로필

baemongs

질문자

2020.03.18

와우 퀵실버만큼 빠른 답변 감사합니다!

이 방법으로 코딩해볼게요~ 될 것 같습니다. 감사합니다.

채널톡 아이콘