zoukankan      html  css  js  c++  java
  • 《与小卡特一起学Python》 Code2

    下边是一个猜数字的小游戏:

    几乎所有语言都这样做的……

    here we go!

     1 import random
     2 secret = random.randint(1,99)
     3 guess = 0
     4 tries = 0
     5 print "AHOY! I'm the Dread Pirate Roberts,and I have a secret!"
     6 print "It is a number from 1 to 99. I'll give you 6 tries."
     7 while guess != secret and tries < 6:
     8     guess = input("What's yer guess?")
     9     if guess < secret:
    10         print "Too low ,ye scurvy dog!"
    11     elif guess > secret:
    12         print "Too high,landlubber!"
    13 
    14     tries = tries + 1
    15 
    16 if guess == secret:
    17     print "Avast! ye got it! Found my secret, ye did!"
    18 else:
    19     print "No more guesses! Better luck next time, matey!"
    20     print "The secret number was",secret

    这是运行效果(猜对了):

    >>> ================================ RESTART ================================
    >>> 
    AHOY! I'm the Dread Pirate Roberts,and I have a secret!
    It is a number from 1 to 99. I'll give you 6 tries.
    What's yer guess?50
    Too low ,ye scurvy dog!
    What's yer guess?75
    Too low ,ye scurvy dog!
    What's yer guess?87
    Too high,landlubber!
    What's yer guess?81
    Avast! ye got it! Found my secret, ye did!
    

    (猜错了)

    >>> ================================ RESTART ================================
    >>> 
    AHOY! I'm the Dread Pirate Roberts,and I have a secret!
    It is a number from 1 to 99. I'll give you 6 tries.
    What's yer guess?1
    Too low ,ye scurvy dog!
    What's yer guess?2
    Too low ,ye scurvy dog!
    What's yer guess?3
    Too low ,ye scurvy dog!
    What's yer guess?4
    Too low ,ye scurvy dog!
    What's yer guess?5
    Too low ,ye scurvy dog!
    What's yer guess?6
    Too low ,ye scurvy dog!
    No more guesses! Better luck next time, matey!
    The secret number was 24
    

      

    令我惊讶的是这一句:

    print "The secret number was",secret
    

      怎么直接就能显示值得呢?感觉异常的怪!

    还有一个,语句后边没有分号,if elif else while 后边要加冒号。

    My God !!! 

    print secret,guess,tries

    这样子居然都行。OK第二个程序运行完毕!

  • 相关阅读:
    ZoneJS 的原理与应用
    RxJS 中的观察者和迭代器模式
    前端三大框架:数据绑定与数据流
    Angular 的前世今生
    验证Kubernetes YAML的最佳实践和策略
    GitOps初阶指南:将DevOps扩展至K8S
    如何使用Istio 1.6管理多集群中的微服务?
    5个规则,确保你的微服务优化运行
    使用Thanos实现Prometheus指标联邦
    丢弃掉那些BeanUtils工具类吧,MapStruct真香!!!
  • 原文地址:https://www.cnblogs.com/ccccnzb/p/5274750.html
Copyright © 2011-2022 走看看