Im making a program that generates a song based on a random number generator. I have that down, but now I want to be able to control the outcome depending on a variable. Ex: variable1 = "eggs"; since variable1 is "eggs" instead of generating numbers 1-78, it only generates numbers 45-78.
我正在制作一个基于随机数生成器生成歌曲的程序。我已经记下了,但现在我希望能够根据一个变量来控制结果。例如:变量1=“鸡蛋”;因为变量1是“鸡蛋”而不是生成数字1-78,所以它只生成数字45-78。
import random
random_number = random.randint(1,56)
if unit == ('olive') or ('eggplant'):
random_number = random.randint(4,8)
unit was an empty string, and when I ran this, it printed only the outputs connected to 4-8
单位是一个空字符串,当我运行此命令时,它只打印连接到4-8的输出
更多回答
优秀答案推荐
Your syntax for the or
statement is wrong.
您的或语句的语法错误。
You could do this, to properly use an or
statement:
您可以这样做,以正确使用或语句:
if (unit == 'olive') or (unit == 'olive'):
pass # your code goes here instead
Or you could do this, to express the same idea with less cluttered syntax:
或者你可以这样做,用更简洁的语法表达同样的想法:
vegetables = ['olive', 'eggplant']
if unit in vegetables:
pass # your code goes here instead
更多回答
我是一名优秀的程序员,十分优秀!