Python帮助(计算机猜数)

时间:2016-10-18 17:49:05

标签: python

我正在学习如何用Python编程,我发现2个任务应该非常简单,但第二个对我来说非常困难。

基本上,我需要制作计算机猜测我的号码的程序。所以我输入一个数字,然后计算机试图猜测它。每次选择号码时,我都需要输入LowerHigher。我不知道该怎么做。谁能告诉我如何做到这一点?

例如(数字为5):
电脑问10?
我写Lower
电脑问4?
我写了Higher

程序: 我已经创建了一个自动显示HigherLower的程序,但我希望以用户身份输入LowerHigher

from random import randit
number = int(input("Number? "))

attempts = 0
guess = 0

min = 0
max = 100

while guess != number:
  guess = randint(min,max)
  print(guess,"?")

  if guess < number:
    print("Higher")
    min = guess
  elif guess > number:
    print("Lower")
    max = guess

   attemps += 1

print("I needed", attempts, "attemps")

4 个答案:

答案 0 :(得分:0)

你可能想要提出一个案例,如果它不匹配你的输入,我认为你需要一个案例,当猜测最终等于数字时,你可能想要输入&#34发现它!&#34;或类似的东西。

from random import randint
number = int(input("Number? "))

attempts = 0
guess = 0

min = 0
max = 100

while guess != number:
  guess = randint(min,max)
  print(guess,"?")

  answer = str(input("Higher/Lower? "))

  if answer == 'Higher':
    min = guess
  elif answer == 'Lower':
    max = guess

  attempts += 1

print("I needed", attempts, "attempts")

答案 1 :(得分:0)

from random import randit 

attempts = 0
guess = 0 
min = 0
max = 100
while guess != number: 
  number = int(input("Number? ")) 
  guess = randint(min,max)
  print(guess,"?") 
  if guess < number:
     print("Higher") 
     min = guess 
  elif guess > number:
      print("Lower") 
      max = guess
  attemps += 1 
print("I needed", attempts, "attemps")

问题是您使用循环但在应用程序启动时输入一次值。只需在while语句中输入输入希望这个帮助

答案 2 :(得分:0)

from random import randint

print('choos a number in your brain and if guess is true enter y else any key choose time of guess: ')

print("define the range (A,B) :")
A = int(input("A: "))
B = int(input("B: "))
time = int(input("time:"))

while time != 0:
    ran = randint(A, B)
    inp = input(f"is this {ran} ?")
    time -= 1
    if inp == "y":
        print("bla bla bla computer wins!")
        break
    print("NOPE!")
    if time == 0:
        print("computer game over!")
        break

答案 3 :(得分:0)

from random import *

number = int(input("Number? "))

attempts = 0
guess = 0

min = 0
max = 100
attemps =0
guess = randint(min,max)
while guess != number:
    userInput=input(str(guess)+"?")
    if userInput.lower()=="lower":
        max=guess
    elif userInput.lower()=="higher":
        min=guess
    attemps += 1
    guess = randint(min,max)

print("I needed", attemps, "attempts to guess ur number ="+str(guess))

输出:

Number? 5
66?lower
63?lower
24?lower
19?lower
18?lower
10?lower
4?higher
9?lower
6?lower
4?higher
I needed 10 attempts to guess ur number =5