Python 猜数字游戏优化(用户创建数字,计算机猜测)
我对编程非常陌生,所以大约4、5天前我决定开始使用Python。我遇到了一个挑战,要求我创建一个“猜数字”游戏。完成后,“困难的挑战”是创建一个猜测数字游戏,用户创建数字,计算机(AI)猜测 到目前为止,我已经想出了这个办法,它是有效的,但它可能会更好,我会解释Python 猜数字游戏优化(用户创建数字,计算机猜测),python,algorithm,python-3.x,Python,Algorithm,Python 3.x,我对编程非常陌生,所以大约4、5天前我决定开始使用Python。我遇到了一个挑战,要求我创建一个“猜数字”游戏。完成后,“困难的挑战”是创建一个猜测数字游戏,用户创建数字,计算机(AI)猜测 到目前为止,我已经想出了这个办法,它是有效的,但它可能会更好,我会解释 from random import randint print ("In this program you will enter a number between 1 - 100." "\nAfter the comp
from random import randint
print ("In this program you will enter a number between 1 - 100."
"\nAfter the computer will try to guess your number!")
number = 0
while number < 1 or number >100:
number = int(input("\n\nEnter a number for the computer to guess: "))
if number > 100:
print ("Number must be lower than or equal to 100!")
if number < 1:
print ("Number must be greater than or equal to 1!")
guess = randint(1, 100)
print ("The computer takes a guess...", guess)
while guess != number:
if guess > number:
guess -= 1
guess = randint(1, guess)
else:
guess += 1
guess = randint(guess, 100)
print ("The computer takes a guess...", guess)
print ("The computer guessed", guess, "and it was correct!")
来自随机导入randint
打印(“在此程序中,您将输入一个介于1到100之间的数字。”
“\n计算机将尝试猜测您的号码!”
数字=0
当数字<1或数字>100时:
number=int(输入(“\n\n输入一个数字以便计算机猜测:”)
如果数量>100:
打印(“数字必须小于或等于100!”)
如果数字小于1:
打印(“数字必须大于或等于1!”)
guess=randint(1100)
打印(“计算机进行猜测…”,猜测)
猜猜看!=编号:
如果猜测>数字:
猜测-=1
guess=randint(1,guess)
其他:
猜测+=1
guess=randint(guess,100)
打印(“计算机进行猜测…”,猜测)
打印(“计算机猜中了”,猜中了,“它是正确的!”)
这是我上次跑步时发生的情况:
输入计算机要猜测的数字:78
计算机猜测。。。七十四
计算机猜测。。。89
计算机猜测。。。五十五
计算机猜测。。。78
电脑猜到78,结果是正确的
请注意,它是有效的,但是当计算机猜测到74时,它猜测到了一个更高的数字,即89。数字太高,因此计算机猜测的数字较低,但选择的数字是55。有没有办法让电脑猜出一个低于89但高于74的数字?这需要额外的变量还是更复杂的if、elif、else语句
谢谢你Ryan Haining
我使用了你回复中的代码,并稍微修改了一下,所以猜测总是随机的。如果你看到这一点,让我知道这是最好的方式这样做
from random import randint
def computer_guess(num):
low = 1
high = 100
# This will make the computer's first guess random
guess = randint(1,100)
while guess != num:
print("The computer takes a guess...", guess)
if guess > num:
high = guess
elif guess < num:
low = guess + 1
# having the next guess be after the elif statement
# will allow for the random guess to take place
# instead of the first guess being 50 each time
# or whatever the outcome of your low+high division
guess = (low+high)//2
print("The computer guessed", guess, "and it was correct!")
def main():
num = int(input("Enter a number: "))
if num < 1 or num > 100:
print("Must be in range [1, 100]")
else:
computer_guess(num)
if __name__ == '__main__':
main()
来自随机导入randint
def计算机猜测(数字):
低=1
高=100
#这将使计算机的第一次猜测变得随机
猜测=randint(1100)
猜猜看!=号码:
打印(“计算机进行猜测…”,猜测)
如果猜测>数值:
高=猜测
elif guess100:
打印(“必须在[1100]范围内”)
其他:
计算机猜测(num)
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
main()
您只需要两个新变量来跟踪下限和上限:
low = 1
high = 100
while guess != number:
if guess > number:
high = guess - 1
else:
low = guess + 1
guess = randint(low, high)
print ("The computer takes a guess...", guess)
您只需要两个新变量来跟踪下限和上限:
low = 1
high = 100
while guess != number:
if guess > number:
high = guess - 1
else:
low = guess + 1
guess = randint(low, high)
print ("The computer takes a guess...", guess)
我简单地制作了您需要的游戏,如下所示:
import random
guess=int(input("Choose a number you want the computer to guess from 1-100: "))
turns=0
a=None
compguess=random.randint(1,100)
while turns<10 and 100>guess>=1 and compguess!=guess: #computer has 10 turns to guess number, you can change it to what you want
print("The computer's guess is: ", compguess)
if compguess>guess:
a=compguess
compguess=random.randint(1,compguess)
elif compguess<guess:
compguess=random.randint(compguess,a)
turns+=1
if compguess==guess and turns<10:
print("The computer guessed your number of:" , guess)
turns+=1
elif turns>=10 and compguess!=guess:
print("The computer couldn't guess your number, well done.")
input("")
随机导入
guess=int(输入(“选择您希望计算机从1-100猜出的数字:”)
圈数=0
a=无
compguess=random.randint(1100)
而turnsguess>=1和compguess=猜:#计算机有10圈来猜数字,你可以把它改成你想要的
打印(“计算机的猜测是:”,compguess)
如果compguess>guess:
a=猜
compguess=random.randint(1,compguess)
elif compguess我简要制作了您需要的游戏,如下所示:
import random
guess=int(input("Choose a number you want the computer to guess from 1-100: "))
turns=0
a=None
compguess=random.randint(1,100)
while turns<10 and 100>guess>=1 and compguess!=guess: #computer has 10 turns to guess number, you can change it to what you want
print("The computer's guess is: ", compguess)
if compguess>guess:
a=compguess
compguess=random.randint(1,compguess)
elif compguess<guess:
compguess=random.randint(compguess,a)
turns+=1
if compguess==guess and turns<10:
print("The computer guessed your number of:" , guess)
turns+=1
elif turns>=10 and compguess!=guess:
print("The computer couldn't guess your number, well done.")
input("")
随机导入
guess=int(输入(“选择您希望计算机从1-100猜出的数字:”)
圈数=0
a=无
compguess=random.randint(1100)
而turnsguess>=1和compguess=猜:#计算机有10圈来猜数字,你可以把它改成你想要的
打印(“计算机的猜测是:”,compguess)
如果compguess>guess:
a=猜
compguess=random.randint(1,compguess)
elif compguess您正在寻找的是经典
那么每一步都发生了什么
low=1
,high=100
=>guess=50
50<82所以low=51
low=51
,high=100
=>guess=75
75<82所以low=76
low=76
,high=100
=>guess=88
88>82所以high=88
low=76
,high=88
=>guess=82
82==82,我们完成了
请注意,这种方法的时间复杂度是O(lg(N))
您正在寻找的是经典的
那么每一步都发生了什么
low=1
,high=100
=>guess=50
50<82所以low=51
low=51
,high=100
=>guess=75
75<82所以low=76
low=76
,high=100
=>guess=88
88>82所以high=88
low=76
,high=88
=>guess=82
82==82,我们完成了
请注意,这个问题的时间复杂度是O(lg(N))
我就是这样做的
__author__ = 'Ghengis Yan'
print("\t This is the age of the computer")
print("\n The computer should impress us... the Man")
import random
#User chooses the number
the_number = int(input("Human Choose a number between 0 and 100 "))
tries = 1
computer = random.randint(0,100)
# User choose again loop
while the_number > 100:
the_number = int(input("I thought Humans are smarter than that... \nRetype the number... "))
if the_number <= 100:
print("Good")
# Guessing Loop
while computer != the_number:
if computer > the_number:
print(computer, "lower... Mr. Computer")
else:
print(computer, "higher... Mr. Computer")
computer = int(random.randint(0,100))
tries += 1
print("Computer Congratulations... You beat the human! The Number was ", the_number)
print("It only took a computer such as yourself", tries, "tries to guess it right... pathetic")
input("\nPress the enter key to exit.")
\uuuuu作者\uuuuu='Ghengis Yan'
打印(“\t这是计算机时代”)
打印(“\n计算机应该给我们留下深刻的印象……男人”)
随机输入
#U
import random
player = int(input("tap any number: "))
comp = random.randint(1, 100)
print(comp)
comp_down = 1
comp_up = 100
raw_input("Press Enter to continue...")
while comp != player:
if comp > player:
comp_up = comp - 1
comp = random.randint(comp_down, comp_up)
print(comp)
if comp < player:
comp_down = comp + 1
comp = random.randint(comp_down, comp_up)
print(comp)
if comp == player:
break
import random
#program allows computer to guess my number
#initial values
user_input1=int(input("Enter number between 1 and 100: "))
tries=1
compguess=random.randint(1, 100)
#guessing loop
while compguess != user_input1:
if compguess > user_input1:
print("Lower Guess")
compguess=random.randint(1, 100)
print(compguess)
elif compguess < user_input1:
print("Higher Guess")
compguess=random.randint(1, 100)
print(compguess)
tries += 1 #to have program add up amount of tries it takes place it in the while block
print("Good job Computer! You guessed it! The number was,", user_input1, \
" and it only took you", tries, " tries!")
max_guess_number = 0
min_guess_number = 0
max_guess_number = guess - 1
computer_guess = random.randint(min_guess_number, max_guess_number)
min_guess_number = guess + 1
computer_guess = random.randint(min_guess_number, max_guess_number)
print 'Please think of a number between 0 and 100!'
low = 0
high = 100
while(True):
rand = (high+low)/2
print 'Is your secret number '+str(rand)+'?'
ans = raw_input("Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly.")
if ans=='h':
high = rand
elif ans=='l':
low = rand
elif ans=='c':
print "Game over. Your secret number was:",rand
break
else:
print "Sorry, I did not understand your input"
import random
corr_num = random.randint(1,100)
player_tries = 0
com_tries = 0
while player_tries <5 and com_tries < 5:
player = int(input("player guess is "))
if player > corr_num:
print("too high")
player_tries +=1
if player < corr_num:
print("too low")
player_tries +=1
if player == corr_num:
print("Player wins")
break
computer = random.randint(1,100)
print("computer guess is ", computer)
if computer > corr_num:
print("too high")
com_tries = 0
if computer < corr_num:
print("too low")
com_tries = 0
if computer == corr_num:
print ("computer wins")
break
else:
print("Game over, no winner")**strong text**
import random
x = 1
y = 99
hads = random.randint(x,y)
print (hads)
javab = input('user id: ')
while javab != 'd':
if javab == 'b':
x = hads
hads = random.randint(x,y)
print(hads)
javab = input('user id: ')
else:
javab == 'k'
y = hads
hads = random.randint(x,y)
print(hads)
javab = input('user id: ')