gpt4 book ai didi

Is there a way to sort outputs depending on a variable? [duplicate](有没有办法根据变量对输出进行排序?[复制])

转载 作者:bug小助手 更新时间:2023-10-28 10:09:29 24 4
gpt4 key购买 nike




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的输出


更多回答

stackoverflow.com/questions/20002503/…

stackoverflow.com/questions/20002503/...

优秀答案推荐

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

更多回答

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com