简体   繁体   English

询问用户(是/否)是否显示情节的问题。 是否使用(if/else)语句运行图形代码块

[英]Asking user (yes/no) question of whether to show a plot. Using (if/else) statements to run block of graphing code or not

I want to ask a user if they would like to see a certain graph.我想问用户是否想看某个图表。 If they say "yes," I want my block of plot code to run.如果他们说“是”,我希望我的情节代码块能够运行。 If they say "no," I want it to return None and go to the next question.如果他们说“不”,我希望它返回None并转到下一个问题。

When I run my cell and input a 'yes' it just goes to the next question.当我运行我的单元格并输入'yes'时,它就会转到下一个问题。 The plot code works outside of the if statement.情节代码在if语句之外工作。 I am on a mac using Jupyter Notebook, running an Anaconda supported Python environment.我在 Mac 上使用 Jupyter Notebook,运行 Anaconda 支持的 Python 环境。

yeses=['yes','Yes','YES','y','Y'] 
nos=['no','No','NO','n','N']

p = input("Would you like to see the signal pattern of the song? (yes/no): ")
f = input("Would you like to see a plot of all the frequencies used in this song? (yes/no): ")
s = input("Would you like to see a spectrogram of the song? (yes/no): ")

if p in yeses:
    plt.subplot(2,1,1)
    plt.plot(time, sound[:,0], 'r')
    plt.xlabel("time, s [left channel]")
    plt.ylabel("signal, relative units")
    plt.subplot(2,1,2)
    plt.plot(time, sound[:,1], 'b')
    plt.xlabel("time, s [right channel]")
    plt.ylabel("signal, relative units")
    plt.tight_layout()
    plt.show()
else:
    return None

I assume you typed: yes CTRL-ENTER我假设你输入了:是的 CTRL-ENTER

Maybe you received "yes\n" ?也许你收到了"yes\n" Use repr(p) to diagnose, and p.rstrip() to fix, stripping any trailing newlines.使用repr(p)进行诊断,使用p.rstrip()进行修复,去除任何尾随的换行符。


You wrote你写了

yeses = ['yes', 'Yes', 'YES', 'y', 'Y']

You could more concisely use你可以更简洁地使用

yeses = ['yes', 'y']

and rely on p.lower() to uniformly downcase all user inputs.并依靠p.lower()统一缩小所有用户输入。

Write a tiny helper function if the code does that repeatedly.如果代码重复执行此操作,请编写一个小的辅助函数。

暂无
暂无

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

相关问题 询问用户他/她是否想再次玩,但输入 yes 只是再次重复这个问题,而不是重新开始 - Asking user whether he/she wants to play again but typing yes just repeats this question again rather starting again 需要帮助来确定用户是否使用Python回答了“是”或“否” - Need help determining whether a user has answered “yes” or “no” using Python 使用 if 和 else 语句的代码未返回正确的 output - Code not returning correct output using if and else statements else语句始终运行 - else statements always run 检查特定位置是否存在多个文件,如果是,则继续执行代码,否则重新运行上述代码 - Check if multiple files are present in particular location if yes then proceed with the code else re run the above code 在 python Django 中,我想运行两条关于 if else 的语句,即如果 email 被取用显示它或如果用户名被取用则显示它 - In python Django I want to run two statements on if else that is if email is taken show it or if username is taken show it 我有一个关于 if else 语句和 raw_input 语句的问题 - I have a question about if else statements and raw_input statements 打印语句在if / elif / else块中不起作用 - Print statements not working in if/elif/else block 将带有True / False语句的if / else块转换为字典 - Converting an if/else block with True/False statements into a dictionary 在最后一个块中同时执行 if 和 else 语句 - Executing both if and else statements in the last block
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM