首页 > 编程语言 > 详细

Python-模块,以及使用文本中的数据

时间:2018-11-28 16:53:00      阅读:198      评论:0      收藏:0      [点我收藏+]

模块导入:

from math import pi as math_pi

print math_pi     #相当于把pi取了个别名

 

# -*- coding: cp936 -*-
from random import randint


#读取文件中的数据
f = file("game.txt")
score = f.read().split()   #相当于放入score这个list里了


#分别存入变量中
game_times = int(score[0])
min_times = int(score[1])
total_times = int(score[2])


#计算游戏的平均轮数,注意浮点数的运用
if game_times > 0:
    #avg_times = total_times / game_times   注意此处这样写会出现什么后果
    avg_times = float(total_times) / game_times  #7/2=3
else:
    avg_times = 0


#输出成绩信息
print("你已经玩了%d次,最少%d轮猜出答案,平均%.2f轮猜出答案") %(game_times,min_times,avg_times)
                                             #%.2f的意思要注意


num = randint(1,100)
print "Guess what is the nember?"


i = 6
while i:
    answer = input()
    if answer < num:
        print("too small")
    else:
        print("too big")
    if answer == num:
        print("Yes, You are right!")
    i=i-1

Python-模块,以及使用文本中的数据

原文:https://www.cnblogs.com/two-peanuts/p/10032654.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!