python日记—if & for

▼魔方 西西 提交于 2020-05-07 13:32:28
number = 10

guess = int(input("Enter an integer:"))

if guess == number:
    print('you guessed it right!')
elif guess > number:
    print('you guessed it higher than that!')
else:
    print('you guessed it lower than that!')
    
for i in range(1, 10):
    print(i)
else:   #执行完for语句之后再执行这条语句
    print('The for loop is over!') 

#遍历表list
a_list = [1, 3, 5, 7, 9]
for i in a_list:
    print(i)

#遍历元组tuple
a_tuple = (2, 4, 6, 8, 10)
for i in a_tuple:
    print(i)
    
#遍历字典
a_dict = {'Tom':3, 'Kim':4, 'Lucy':5}
for ele in a_dict:
    print(ele)
    print(a_dict[ele])    

for key, elem in a_dict.items():
    print(key, elem)
    
    

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!