## python 杂例

### 猜数字

from random import randint
num = randint(1, 100)

print('Guess what I think?')
bingo = False
count = 0

while bingo == False:
count += 1
bingo = True

print(count)

### 输出-1

* * * * *
* * * * *
* * * * *
* * * * *
* * * * *

for i in range(0, 5):
for p in range(0, 5):
print('*',end='')
print()

### 输出-2

*
**
***
****
*****

n=0
for i in range(0, 5):
for p in range(0, 1+n):
print('*',end='')
n=n+1
print()

### 猜数字

def isEqual(num1, num2):
if num1<num2:
print ('too small')
return False
if num1>num2:
print ('too big')
return False
if num1==num2:
print ('bingo!')
return True

from random import randint
num = randint(1, 100)
print('Guess what I think?')
bingo = False
while bingo == False:
bingo = isEqual(answer, num)

### if的嵌套

if y >= 0:
if x >= 0:
print (1)
else:
print (2)
else:
if x < 0:
print (3)
else:
print (4)


### 点球小游戏

from random import choice
print ('Choose one side to shoot:')
print ('left, center, right')
you = input()
print ('You kicked ' + you)
direction = ['left', 'center', 'right']
com = choice(direction)
print ('Computer saved ' + com)
if you != com:
print ('Goal!')
else:
print ('Oops...')

from random import choice

score_you = 0
score_com = 0
direction = ['left', 'center', 'right']

for i in range(5):
print ('==== Round %d - You Kick! ====' % (i+1))
print ('Choose one side to shoot:')
print ('left, center, right')
you = input()
print ('You kicked ' + you)
com = choice(direction)
print ('Computer saved ' + com)
if you != com:
print ('Goal!')
score_you += 1
else:
print ('Oops...')
print ('Score: %d(you) - %d(com)\n' % (score_you, score_com))

print ('==== Round %d - You Save! ====' % (i+1))
print ('Choose one side to save:')
print ('left, center, right')
you = input()
print ('You saved ' + you)
com = choice(direction)
print ('Computer kicked ' + com)
if you == com:
print ('Saved!')
else:
print ('Oops...')
score_com += 1
print ('Score: %d(you) - %d(com)\n' % (score_you, score_com))

from random import choice

score = [0, 0]
direction = ['left', 'center', 'right']

def kick():
print('==== You Kick! ====')
print('Choose one side to shoot:')
print('left, center, right')
you = input()
print('You kicked ' + you)
com = choice(direction)
print('Computer saved ' + com)
if you != com:
print('Goal!')
score[0] += 1
else:
print('Oops...')
print('Score: %d(you) - %d(com)\n' % (score[0], score[1]))

print('==== You Save! ====')
print('Choose one side to save:')
print('left, center, right')
you = input()
print('You saved ' + you)
com = choice(direction)
print('Computer kicked ' + com)
if you == com:
print('Saved!')
else:
print('Oops...')
score[1] += 1
print('Score: %d(you) - %d(com)\n' % (score[0], score[1]))

for i in range(5):
print('==== Round %d ====' % (i + 1))
kick()

while (score[0] == score[1]):
i += 1
print('==== Round %d ====' % (i + 1))
kick()

if score[0] > score[1]:
print('You Win!')
else:
print('You Lose.')

### 处理文件中的数据

f = open('scores.txt', encoding='gbk')
# print(lines)
f.close()
results = []

for line in lines:
# print (line)
data = line.split()
# print (data)

sum = 0
score_list = data[1:]
for score in score_list:
sum += int(score)
result = '%s \t: %d\n' % (data[0], sum)
# print (result)

results.append(result)

# print (results)
output = open('result.txt', 'w', encoding='gbk')
output.writelines(results)
output.close()

### 查天气

import requests
while True:
city = input('请输入城市,回车退出:\n')
if not city:
break
try:
req = requests.get('http://wthrcdn.etouch.cn/weather_mini?city=%s' % city)
except:
print('查询失败')
break

#print(req.text)
dic_city = req.json()  #转换成字典
#print(dic_city)

city_data = dic_city.get('data') #没有‘data’的话返回None
if city_data:
city_forecast = city_data['forecast'][0]  #下面都可以换成get的方法
print(city_forecast.get('date'))
print(city_forecast.get('high'))
print(city_forecast.get('low'))
print(city_forecast.get('type'))
else:
print('未获得')

### 输入工资(注意else用法)

salarySum = 0
salarys =[]
for i in range(4):
s = input("请输入一共4名员工的薪资(按Q或q中途结束)")

if s.upper() == "Q":
print("录入完成，退出")
break
if float(s) < 0:
continue

salarys.append(float(s))
salarySum += float(s)

else:      # else语句，如果前面出现了Q/q(提前跳出循环)，那么才不执行else
print("您已经全部录入4名员工的薪资")

print("录入平均薪资: ",salarys)
print("平均薪资{0}".format(salarySum/4))