python3练习:while循环

# -*- coding:utf-8 -*-
#while 练习

'''
   @ 练习1:输入不同的能量来源,显示不同的能量值。
   @ author:jll
   @ create:2019-11-20
'''
while True:
    info = input('\n查询能量请输入能量来源!退出程序请输入0\n能量来源如下:\n生活缴费、行走捐、共享单车、线下支付、网络购票\n') # 获取控制台输入的内容
    if info=='生活缴费':  # 判断输入的能量来源
        print('180g')     # 打印对应的能量
    elif info == '行走捐':
        print('200g')
    elif info=='共享单车':
        print('80g')
    elif info=='线下支付':
        print('5g')
    elif info=='网络购票':
        print('80g')
    elif info=='0':
        print('已退出!')
        break
    else:                       #输入非法内容,退出
        print ("输入错误!")
        break


'''
   @ 练习2:猜1~100之内的数字。
   @ author:jll
   @ create:2019-11-20
'''
import random
j = random.randint(1,101)
print ("我现在心里想到了一个数字,请你猜一下是多少?")
while True:
    x0 = input("你猜的数字是:")                  #接收录入内容
    if x0.isdigit():                            #确认录入内容为数字
        x = int(x0)                             #录入内容为str类型,需转为int类型
        if x > 100 or x < 1:
            print ("1~100之内的数字!说啥呢?重来!\n")
            break
        elif x == j:
            print ("真棒!你猜对了~\n数字就是%d,游戏结束" %x)   #猜对了,游戏结束
            break
        elif x > j:
            print ("大了,往小了说~\n")
        elif x < j:
            print ("小啦,往大了说~\n")
    else:
        print ("数字呀!写啥呢~?重来!\n")               #录入内容不为数字则退出
        break

结果(其中一部分)

C:\Users\tians\AppData\Local\Programs\Python\Python36\python.exe D:/01Test/scripts/mianshi-test/python_LX.py

查询能量请输入能量来源!退出程序请输入0
能量来源如下:
生活缴费、行走捐、共享单车、线下支付、网络购票
东方闪电
输入错误!



我现在心里想到了一个数字,请你猜一下是多少?
你猜的数字是:水电费水电费
数字呀!写啥呢~?重来!


Process finished with exit code 0

猜你喜欢

转载自www.cnblogs.com/jxba/p/11899467.html