2018-10-31

2018-11-01  本文已影响0人  自语自乐

Python交互式程序与While循环

(1)用户输入——input()

message=input('show me a number and I will tell you if it is an odd number: ')

number=int(message)

if number % 2==0:

    print(str(number)+' is not an odd number!')

else:

    print(str(number)+' is an odd number!')

(2)While循环

msg='please enter what you want to eat: '

msg+='\n(enter "quit"when you have finished)'

active=True

food=[]

while active:

    information=input(msg)

    if information!='quit':

        food.append(information)

    else:

        active=False

print('you have ordered the following items:')

for f in food:

    print(f)

(3)break语句

msg='please enter what you want to eat: '

msg+='\n(enter "quit"when you have finished)'

food=[]

while True:

    information=input(msg)

    if information!='quit':

        food.append(information)

    else:

        break

print('you have ordered the following items:')

for f in food:

    print(f)

(4)continue语句

digits=range(10)

for d in digits:

    if d %2 ==0:

        continue

    print(d)

上一篇下一篇

猜你喜欢

热点阅读