这个游戏的目标是在三次尝试中猜出一个数字,如果你做对了,请恭喜你,如果你弄错了,请告诉你“不”。我认为我的代码是正确的,但我是编程的新手并且不确定。我正在测试它是否有效,但这可能需要很长时间。任何帮助表示赞赏!
# Guess My Number
#
# The computer picks a random number between 1 and 100
# The player tries to guess it and the computer lets
# the player know if the guess is too high, too low
# or right on the money
import random
print("\tWelcome to 'Guess My Number'!")
print("\nI'm thinking of a number between 1 and 100.")
print("Try to guess it in as few attempts as possible.\n")
# set the initial values
the_number = random.randint(1, 100)
guess = int(input("Take a guess: "))
tries = 1
# guessing loop
while guess != the_number:
if guess > the_number:
print("Lower...")
elif tries == 3:
break
else:
print("Higher...")
guess = int(input("Take a guess: "))
tries += 1
if guess == the_number:
print("You guessed it! The number was", the_number)
print("And it only took you", tries, "tries!\n")
input("\n\nPress the enter key to exit.")
if tries == 3:
print("no")
input("\n\nPress the enter key to exit.")
答案 0 :(得分:0)
要测试您的代码,请在进入while循环之前尝试打印存储在the_number = random.randint(1, 100)
中的数字。例如:
# Guess My Number
#
# The computer picks a random number between 1 and 100
# The player tries to guess it and the computer lets
# the player know if the guess is too high, too low
# or right on the money
import random
print("\tWelcome to 'Guess My Number'!")
print("\nI'm thinking of a number between 1 and 100.")
print("Try to guess it in as few attempts as possible.\n")
# set the initial values
the_number = random.randint(1, 100)
print("The number is: " + str(the_number))
guess = int(input("Take a guess: "))
tries = 1
# guessing loop
while guess != the_number:
if guess > the_number:
print("Lower...")
elif tries == 3:
break
else:
print("Higher...")
guess = int(input("Take a guess: "))
tries += 1
if guess == the_number:
print("You guessed it! The number was", the_number)
print("And it only took you", tries, "tries!\n")
input("\n\nPress the enter key to exit.")
if tries == 3:
print("no")
input("\n\nPress the enter key to exit.")
答案 1 :(得分:0)
将代码包装在函数中。
def guessing_game(number=None):
if number is None:
number = int(input("Take a guess: "))
... continue the rest of your code.
# Probably want a return True or a return false
# end guessing_game
通过创建单元测试来测试您的代码
import unittest
class GuessTest(unittest.TestCase):
def setUp(self):
"""Setup variables. This runs before every test method."""
self.number = random.randint(1, 100)
# end setUp
def test_guess(self):
"""Run the test for the guessing game."""
result = guessing_game(self.number)
myvalue = result # You want this to be the expected result
# check result - for your game you may just want to run a loop
self.assertEqual(result, myvalue, "Hey this doesn't work")
# end test_guess
def test_other_method(self):
pass
def tearDown(self):
"""This runs after every test is finished."""
# If you have to clean up something
pass
# end tearDown
# end class GuessTest
if __name__ == "__main__":
# Code that runs when you run this file, so importing this file doesn't run your code
# guessing_game() # Run your function
unittest.main() # Run the unittests
其他注意事项:如果您知道要循环的次数,请使用for循环。
for i in range(3):
print(i) # Will print 0, 1, 2
if guess > the_number:
print("Lower ...")
elif guess < the_number:
print("Higher ...")
else:
break # You are correct
答案 2 :(得分:0)
import random
n = random.randrange(1, 11)
count=0
print('I\'ve though a number between 1 and 10!')
while True:
try:
g = input('Guess Any Number!')
g = int(g)
if not 10>g>0:
if count==6:
print ("You Loose")
print('It\'s in between 0 and 10!')
except ValueError:
print('Enter an integer')
continue
if g == n:
print('Congratulations You Gussed That Number!')
break
if g < n:
print('Larger Than The Guessed One')
if g > n:
print('Smaller Than The Guessed One')
count +=1