提问人:Journee 提问时间:12/26/2022 更新时间:12/26/2022 访问量:58
编码和恶作剧的新朋友一无所知,但我的脚本输入中断了
New to coding and pranking friend who knows nothing, but my script input breaks
问:
我知道这听起来很愚蠢,但我正在做一个烦人的婴儿模拟,它会反复提出问题,除非给出准确的输入响应来恶作剧朋友。
我有一组随机问题和随机的后续问题要提供给他,而他会努力工作,直到他基本上发现“密码”输入。然而,我的后续问题不会随机化,而是只从列表中选择一个,然后完全破坏代码,使新的输入提示无限地一遍又一遍地成为随机字符/元素。
我尝试在 while 循环中放置另一个 while 循环,以断言后续问题拉取的更明确的随机性,并希望解决输入提示问题,但我什至不确定您是否可以成功完成这样的事情,但它不起作用,所以我删除了它,我仍然完全不熟悉编码。
尽管这个想法看起来相对简单,但我可能只是不知所措,所以解决方案可能是我还没有学到的东西,但这是我无法修复的愚蠢的恶作剧脚本:
from random import choice
new_questions = ["Where are all the dinosaurs?: ", "How are babies made?: ", "Why don't we have superpowers?: ", "Why is the sky blue?: "]
questions = ["Well why is the sky blue then?!: ", "Why though?: ", "I still don't get it...why?: ", "OHH OKAY...no, no, wait, but...Why?: ", "WHY?!: ", "You suck at explaining things...just tell me why already.: ", "Why?: ", "What does that have to do with the sky being blue...?: ", "Yeah, I get that part, but why?: ", "Ok, why?: "]
new_questions = choice(new_questions)
answer = input(new_questions).strip().lower()
while answer != "just because":
questions = choice(questions)
answer = input(questions).strip().lower()
我还没有完成它,因为我仍在试图理解为什么第一部分会中断。
运行后,您会看到它很好地执行了大部分内容,但它不能从我的变量“问题”中随机随机多次随机选择,并且在第一次从问题列表中提取后也会中断,因此只会要求输入一个单数字符元素。
答:
2赞
Matheus Delazeri
12/26/2022
#1
您通过为问题列表分配一个新值(选择)来覆盖问题列表。将其更改为:questions = choice(questions)
while answer != "just because":
question = choice(questions)
answer = input(question).strip().lower()
评论
1赞
Journee
12/26/2022
这绝对像我最初打算的那样工作,非常感谢。作为较新的人,我忽略了重要的小事情,例如覆盖和缺少逗号等,再次感谢您的修复!
1赞
President James K. Polk
12/26/2022
#2
您不想这样做,因为这会用一个随机选择的问题替换问题列表。取而代之的是,类似questions = choice(questions)
while answer != "just because":
question = choice(questions)
answer = input(question).strip().lower()
请注意,它现在不受干扰。questions
评论
0赞
Journee
12/26/2022
这很有道理,我现在完全明白了,非常感谢。
评论