1. Problem description
<font color=green size=4> Requirements describe :</font>- <font color=black size=4> Randomly generate one 100 Integer within , share 10 One chance to start the game , Enter the number of guesses
- <font color=black size=4> If the guess is small , The hint is too small
- <font color=black size=4> If you guess big , Hint guess big
- <font color=black size=4> Guessed it , You guessed right , And end the game
- <font color=black size=4> 10 You haven't guessed right when you run out of opportunities , The end of the game , Didn't guess .</br></br>
- <font color=#0099ff size=4> After the partners read the problem description , Be sure to practice yourself first , Then go to see the blogger's code and problem-solving ideas , In order to improve their programming level , It all depends on self-consciousness !!!
- <font color=red size=4> You are welcome to leave your thoughts or answers in the comment area , Bloggers will choose an optimal solution to top .
2. Algorithm ideas
,
<font color=black size=4>1. Use random Library randint Function to generate a 100 Random number within .<br /><font color=black size=4>2. Use for To carry on a cycle 10 Time , Enter the number to guess at each prompt , Guess big or small, make the corresponding prompt , And output, there are still a few opportunities left .<br /><font color=black size=4>3. When in 10 Guess the right number in one chance , You guessed right , you are so good , Only a few chances .<br /><font color=black size=4>4. 10 You haven't guessed right when you run out of opportunities , The end of the game , I can't guess .3. Code implementation
The way 1: The limit is ten guesses
Implementation code :
"""
Python Practice every day
Guess number games
random library randint How to use
random.randint(a,b) Generate a [a,b] Integer between ( contain a、b)
"""
import random
# Randomly generate one 1-100 Integer between
number = random.randint(0, 100)
# The cycle ends ten times
for i in range(10):
# input Function to enter the number to guess from the keyboard , And converted to int type
choice = int(input(" Please enter the number you want to guess :"))
# Greater than the target number
if choice > number:
print(" You guessed big ")
# Less than the target number
elif choice < number:
print(" You guess it's small ")
# Equal to the target number
else:
print(" You guessed it , That's great !")
print(f" You used {i + 1} Second chance ")
break
print(f" And then there were {9 - i} Second chance ")
else:
print(" The game is over, you haven't guessed .")
Running results :
I guessed ten times before I lost my luck The way 2: Unlimited guess
Implementation code :
"""
Python Practice every day
Guess number games
random library randint How to use
random.randint(a,b) Generate a [a,b] Integer between ( contain a、b)
"""
import random
# Randomly generate one 1-100 Integer between
number = random.randint(0, 100)
# Count the number of guesses
count = 0
while True:
# Every cycle ,count Add 1
count += 1
# input Function to enter the number to guess from the keyboard , And converted to int type
choice = int(input(" Please enter the number you want to guess :"))
# Greater than the target number
if choice > number:
print(" You guessed big ")
# Less than the target number
elif choice < number:
print(" You guess it's small ")
# Equal to the target number
else:
print(" You guessed it , That's great !")
print(f" You used {count} Second chance ")
break
Running results :
The way 3: Let's enter the answer and the computer will guess
Next, let's make a small change to the game , Enter a number by the user , Let the computer guess :
Implementation code :
"""
Python Practice every day
Guess number games
random library randint How to use
random.randint(a,b) Generate a [a,b] Integer between ( contain a、b)
"""
import random
# Enter a correct answer
answer = int(input(' Please input the answer :'))
# start Indicates the starting value of the range
start = 1
# end Indicates the end value of the range
end = 100
# count Still used to count the number of times
count = 0
# The first value guessed by the computer program , Let's get out of the loop first
guess = random.randrange(start, end)
# Every cycle , Change the scope
while True:
count += 1
if guess > answer:
print(f' Computer guess {guess}, Guess the ')
end = guess
guess = random.randrange(start, guess)
elif guess < answer:
print(f' Computer guess {guess}, Guess a little ')
start = guess + 1
guess = random.randrange(start, end)
else:
print(f' right key :{answer}, Computer guess :{guess}: The computer guessed :{count} I guessed right ')
break
Running results :
4. How to make question brushing more efficient ?
<font color=black size=5>1. Programming white contestant </font></br><font color=black size=3> Many novice programmers have learned basic grammar , But I don't know the purpose of grammar , I don't know how to deepen the image , I don't know how to improve myself , This is the time <font color=red size=3> It's very important to brush one question independently every day ( Refining into a God ), You can go to the introductory training of programming beginners on Niuke online .</font> This topic is at the entry level of programming , Suitable for Xiaobai who has just learned grammar , The topic involves basic grammar of programming , Basic structure, etc , Each question has practice mode and examination mode , The test mode can be restored for simulation , You can also practice through practice mode .</font></br> Link address : Cattle from | Beginner programming training <font color=black size=5>2. Advanced programming player </font></br><font color=black size=3> When you have gradually mastered the key points of knowledge after basic practice , Go to this time <font color=red size=3> Learn data structures in special exercises 、 Algorithm basis 、 Fundamentals of computer </font> etc. . Start with the simple , If you feel up, do it in medium difficulty , And more difficult topics .<font color=red size=3> These three are the knowledge points that must be tested in the interview </font>, We can only insist on practicing more every day by ourselves , Refuse to lie flat and continue to brush questions , Continuously improve yourself to impact a satisfactory company .</font></br> Link address : Cattle from | Special exercises <font color=black size=3> Speed up , Let's attack the big factory together , If you have questions, leave a message in the comment area to answer !!!