🐍 파이썬/파이썬 연습문제

[random/inf loop] guessing game

써니(>_<) 2022. 7. 17. 07:26

문제 : Assign a random value to a variable and ask the user to guess the value. If the guess is lower than the chosen value, print ‘too low’, if it is greater, print ‘too high’. In case the number is correct, ‘correct’ and terminate the program.

 

take home: random library와 while True / break 구문을 활용한 infinite loop 작성하기 

 

import random
rnd = random.randint (0, 100) 

while True:
	guess = int input("guess between 0 to 100 : ")
    
	if guess < rnd:
		print("up!")
	elif guess > rnd:
		print("down!")
    else:
    	pirnt("correct :D")
        break