zoukankan      html  css  js  c++  java
  • python游戏练手--猜数字

    #!/usr/bin/env python
    #coding=utf-8

    import random

    #将玩家猜测过的数字保存到这个变量中,因为玩家还没有做过任何猜测,所以保存的是整数0
    guessesTaken = 0

    print('Hello! What is your name?')
    MyName = input()

    #判断输入到MyName中的值是否存在
    #不存在的话,就告诉用户需要输入字符串,不能直接回车或者空格代替
    if MyName == "":
    print('Hello! What is your name? please input string')
    MyName = input()
    else:
    #如果没有问题,直接输出客户的输入到控制台,让客户确认
    print('Your name is ' + MyName)

    #生成一个随机数并存入number变量中
    number = random.randint(1, 20)

    print('Well, ' + MyName + ', I am thinking of a numbert betwen 1 and 20.')
    for i in range(1,7):
    print('Take a guess.')
    # guess = input() #用户输入字符串
    # guess = int(guess) #将用户输入的字符串转变为整数格式
    #要灵活使用int(),float(),str()和bool()函数
    #将上面2个命令组合起来使用,如下guess = int(input())
    #捕捉异常,猜数字的时候如果没有接收到数字,需要重新输入
    try:
    guess = int(input())
    if guess < number:
    print('Your guess is too low.')
    if guess > number:
    print('Your guess is too high.')
    if guess == number:
    break
    except ValueError:
    print('please input number use int')
    guess = int(input())


    #记录猜的次数,配合下面的guessesTaken输出
    guessesTaken += 1

    if guess == number:
    guessesTaken = str(guessesTaken + 1 )
    print('Good Job, ' + MyName + '! You guessed my number in ' + guessesTaken + ' guesses!')


    if guess != number:
    number = str(number)
    print('Nope. The number I was thinking of was ' + number + '.')

    
    
  • 相关阅读:
    URAL——DFS找规律——Nudnik Photographer
    URAL1353——DP——Milliard Vasya's Function
    URAL1203——DPor贪心——Scientific Conference
    递推DP HDOJ 5389 Zero Escape
    区间DP UVA 1351 String Compression
    树形DP UVA 1292 Strategic game
    Manacher HDOJ 5371 Hotaru's problem
    同余模定理 HDOJ 5373 The shortest problem
    递推DP HDOJ 5375 Gray code
    最大子序列和 HDOJ 1003 Max Sum
  • 原文地址:https://www.cnblogs.com/fatyao/p/11223667.html
Copyright © 2011-2022 走看看