简体   繁体   中英

If statement doesn't take place

I have been testing some my code and for some reason my if statement is being ignored. The first if statement works but the second if statement doesn't, i have tried changing it to elif and it still doesn't work. Thanks in advance.

import random


diff = input("What is the ritual difficulty? ")
level = input("How many ritual levels do you have that pertain to this ritual? ")

bag = []

for success in xrange(10):
    bag.append("Success")

bag.append("Flaw")
bag.append("Fail")

extra = level - diff

if extra >= 1:
    extra = extra / 2

    int(extra)
    for chance in xrange(extra):
        bag.append("Success")

if extra < 0:   
    for chance in xrange(extra):
        bag.append("Flaw")
        bag.append("Fail")
        bag.append("Backlash")

print bag

random.shuffle(bag)

outcome = bag.pop()

print "The outcome of the ritual is: ", outcome

You second if will be entered if the diff is larger than the level . However, even if it does get entered it won't actally do something:

if extra < 0:   
    for chance in xrange(extra):
        bag.append("Flaw")
        bag.append("Fail")
        bag.append("Backlash")

The xrange function will yield an empty "list" (it's not actually a list, as the documentation for xrange explains) for negative values, ie nothing will be appended to the bag.

It is not ignored, but when you run a for loop on xrange(extra) with extra being negative, then obviously this loop is immediately terminated.

PS: you might wanna try xrange(-extra) instead...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM