自娱自乐一

自娱自乐系列一:

为了过年提前回家,周六过来加班调休,又不大想干活,就想着捣鼓点自娱自乐的东西:

输入年龄判断是否有投票资格:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
def judge_age():

while True:
try:
age = int(input("\nPlease enter your age:"))
except ValueError:
print("Sorry,I can't understand what you entered.Please enter a correct number.")
continue
else:
break
if age >= 18:
print("You are able to vote in the United States!")
else:
print("You are too young to vote in the United States!")

if __name__ == '__main__':
judge_age()

判断输入的正整数:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

def get_non_negative_int(prompt):
try:
value = int(input(prompt))
except ValueError:
print("Sorry,I can't understand that.")
return get_non_negative_int(prompt)

if value < 0:
print("Sorry,your response must not be negative.")
return get_non_negative_int(prompt)
else:
return value

def guess_number():
number = get_non_negative_int("Please enter a number to guess: (only positive integer can work,thanks!)")
while True:
guess = get_non_negative_int("guess the number.")
if guess > number:
print("You could enter a lower one.")
elif guess < number:
print("I think it could be higher.")
else:
print("Congratulations! You got it!")
break


if __name__ == '__main__':
guess_number()

纯属自娱自乐,毫无技术含量 Thanks?(?ω?)?

顺便贴个常识坑:

文件夹不能取名是**code**,否则pycharm的debug模式会报错!!!(stackoverflow真是个好网站!什么坑都能找到填的办法。。)
原因:
debug模式会引个包:

1
from code import InteractiveConsole 

工程中有code文件夹,会找不到debug需要的方法

文章目录
  1. 输入年龄判断是否有投票资格:
  2. 判断输入的正整数:
    1. 顺便贴个常识坑:
|