小编But*_*ers的帖子

if-if-else vs if-elif-else语句

即使选择了正确的选择,为什么'else'语句也会被打印出来?

    ch=0
    print"Do you need Ice-Cream?"
    ans=raw_input()
    if ans=='y':
        print"Pick a flavor"
        ch=raw_input() # ch is a string
        if ch=='1':
            print"Vanilla"
        if ch=='2':
            print"Chocolate"
        if ch=='3':
            print"Strawberry"
        if ch=='4':
            print"Kiwi"
        if ch=='5':
            print"orange"
        if ch=='6':
            print"mango"
        if ch=='7':
            print"pineapple"
        if ch=='8':
            print"grapes"               
        print"You are done picking up a flavor, pay now"    
    if ans=='n':
        print"Good, you can go!"
    else:
        print"wrong choice" 
Run Code Online (Sandbox Code Playgroud)

即使选择了有效选项,输出也会打印"错误选择".

python if-statement

-2
推荐指数
1
解决办法
1035
查看次数

标签 统计

if-statement ×1

python ×1