简体   繁体   English

Python 随机使用 if 语句

[英]Python using random with if statements

enter image description here在此处输入图像描述

I tried making a sort of text-based game: but I haven't been able to get the first part to work - as it seems to ignore the statement qualifier and does the statement anyway or at least repeats it.我尝试制作一种基于文本的游戏:但我无法让第一部分工作 - 因为它似乎忽略了语句限定符并且无论如何都会执行该语句或至少重复它。 Help would be most appreciated.帮助将不胜感激。 Ignore the odds they are place holders.忽略他们是占位符的可能性。

import random
import math
php=30
while php>0:
    r=random.randint(1,10)
    if r==7:
        print("Found a room with a chest")
        o=input("do you open it?(Y/N)")
        t=random.randint(1,20)

        if o=="Y":
            l=random.randint(1,2)
            lq=random.randint(1,8)
            print(l,"item")
            print(lq,"quality")
            if l==1:
                item="weapon"
            if l==2:
                item="armour"

            if lq == 1 or 2:
                q="bad"
                print(lq,q)
                if item =="weapon":
                    d=random.randint(0,2)
                    dice=4
                    print(q)
                    print(random.randint(1,dice)+random.randint(1,dice)+d)

                if item =="health":
                    armour=random.randint(1,3)

            if lq == 3 or 4 or 5 or 6 or 7:
                q="good"
                print(lq,q)
                if item =="weapon":
                    da=random.randint(1,3)
                    dice=6
                    print(q)
                    print(random.randint(1,dice)+random.randint(1,dice)+d)

                if item =="armour":
                    armour=random.randint(2,6)

            if lq == 8:
                q="rare"
                print(lq,q)
                if item =="weapon":
                    d=random.randint(2,5)
                    dice=random.randint(6,8)
                    print(q)
                    print(random.randint(1,dice)+random.randint(1,dice)+d)

                if item =="armour":
                    armour=random.randint(4,8)
                ```

The problem is that there are places where you are not evaluating anything that could be falsey in some of your if statements.问题是有些地方你没有评估任何可能在你的一些if语句中是错误的东西。

For example, you have this line:例如,您有这一行:

if lq == 3 or 4 or 5 or 6 or 7

The values of 4 , 5 , 6 , and 7 are not expressions to evaluate; 4567的值不是要计算的表达式; they are simply values, which causes python to enter the if block.它们只是值,这会导致 python 进入 if 块。

Instead, you will need to either break out the or statements into expressions to evaluate:相反,您需要将 or 语句分解为表达式以进行评估:

if lq == 3 or lq == 4 or lq == 5 or lq == 6 or lq == 7

Or, the more pythonic way, is to make use of in to check if a value is in a list ( in can evaluate types other than a list, but that's not important right now).或者,更 Pythonic 的方法是使用in检查值是否在列表中( in可以评估列表以外的类型,但这现在并不重要)。 You would do it like this:你会这样做:

if lq in [3, 4, 5, 6, 7]

Edit: Axiumin_ pointed out that this scenario of checking if your value is within a range (as opposed to discrete values) can be evaluated as simply as:编辑: Axiumin_ 指出,这种检查您的值是否在一个范围内(与离散值相反)的场景可以简单地评估为:

if lq >= 3 and lq <= 7

Another pythonic and simple way to evaluate a range, is to use the range function:另一种评估范围的pythonic和简单方法是使用range function:

if lq in range(3, 7)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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