构建一个游戏,以便出现两个随机数,用户必须选择哪一个更大

时间:2015-07-27 21:55:11

标签: python

AIM:构建一个游戏,以便出现两个随机数,用户必须选择哪一个更大 这就是我所拥有的,但我不知道如何制作它,以便代码可以实现,如果用户猜到了更大的数字。

#Ask the user to input his/her name in which will be used in the
#opening comments about the game and how to play it

user_name=str(input("Please type in your name: "))
#print instructions
print('''Hello {}! Welcome!
This game is going to develop your number skills! 

So here is how to play:
Firstly, {}, we are going to give you two numbers.
Then you must choose which of these numbers you think is the biggest.
Type this number in and we will tell you if you are right.
Get enough right and you can progress TO THE NEXT LEVEL!!!''' .format(user_name, user_name))

#RUN MODULE TO CHECK IF THE TEXT IS BEING PRINTED AND THE USERS NAME IS BEING SHOWN IN SPACE OF THE {}

#level 1

#import a random number
import random

a1 = random.randint(1, 10)
a2 = random.randint(1, 10)

#Making sure the integers are not the same
while a1 == a2:
    a2 = random.randint(1, 10)

print('''
The two random values are {} and {}.
Which one do you think is bigger? ''' .format(a1, a2))

#RUN MODULE TO CHECK IF THE IF TWO NUMBERS ARE BEING PRODUCED AND ARE DIFFERENT, MAKING SURE THESE PRINT THROUGH THE THE PRINT STATEMENT.

3 个答案:

答案 0 :(得分:0)

您可以做的最简单的事情是询问最大数字的值,并将其与最大数字进行比较:

biggest = max(a1, a2)

user_num = int(input("What is the biggest number?"))

if user_num == biggest:
    print('Correct!')
else:
    print('Wrong! The biggest number is {}'.format(biggest))

注意在测试相等性之前使用int()将输入转换为整数。

答案 1 :(得分:0)

您询问用户的问题是选择两种可能的结果之一(较小的结果)。如果第一个数字大于第二个数字,你可以说输入1。如果没有。

k=raw_input("Enter 1 if first number bigger, else return: ")

问题是两种结果之间的选择。 您可以稍微改变您的问题,使用随机函数生成1或0,然后询问;是一个神秘的数字我选择1或零输入1或0

答案 2 :(得分:-2)

# do this code 
a = raw_input()
# gets user input
if a1 > a2:
   if a == a1:
      print ('you got it right')
      break
# all the ifs check if it is right or wrong 
   if a != a1:
      print ('you got it wrong try again')
      a = raw_input() 
# then right this same code again but change if a1 > a2: do if a2 > a1: sorry if it does not work