Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit afab9da

Browse files
Guessing game done
1 parent 28c625c commit afab9da

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

‎5-GuessingGame.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# 5 - Guessing game
2+
# Source: https://adriann.github.io/programming_problems.html
3+
# Write a guessing game where the user must guess a secret number. After every guess the program tells the user whether their number was too large or too small. At the end the number of tries needed should be printed. It counts only as one try if they input the same number multiple times consecutively.
4+
5+
from random import randint
6+
7+
print("Guess a number between 1 and 50.")
8+
secretNum = randint(0,50)
9+
# Generate random int adapted from https://stackoverflow.com/questions/3996904/generate-random-integers-between-0-and-9
10+
11+
12+
# Take input from the user and convert to int
13+
userGuess = int(input("Your guess: "))
14+
15+
# While guess is wrong, take more inputs
16+
while userGuess != secretNum:
17+
if userGuess < secretNum:
18+
print("Too low, try again.")
19+
userGuess = int(input())
20+
21+
elif userGuess > secretNum:
22+
print("Too high, try again.")
23+
userGuess = int(input())
24+
25+
# If guess is right, tell user
26+
if userGuess == secretNum:
27+
print("You're right!")

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /