首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何解决python中的“代码不可达”问题?

问如何解决python中的“代码不可达”问题?
EN

Stack Overflow用户
提问于 2022-06-28 19:28:09
回答 2查看 115关注 0票数 -1

这是我最近开始编写的代码:

代码语言:javascript
复制
import random

Q1 = input('''would you like to put your own words instead 
of heads or tails if so say yes if not say no, 
if you said no i will exit the code?''')

if 'Q1 = yes':
   
 Q2 = input('''what word(s) or numbers(s) would 
    you like to use in your random 
    word or number picker?(also no 
    spaces in between numbers.)''')
    y = random.choice(list(Q2))
    print(y)

elif 'Q1 = no':

    print(bye)
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-06-28 19:33:11

看看这一行:

代码语言:javascript
复制
if 'Q1 = yes':

请注意,您正在使用字符串作为if条件。因此,它始终是一个True语句。

相反,请使用以下内容:

代码语言:javascript
复制
if Q1 == 'yes':
票数 1
EN

Stack Overflow用户

发布于 2022-06-28 19:35:22

有许多语法错误,更正如下:

代码语言:javascript
复制
import random

Q1 = input(
    """would you like to put your own words instead 
of heads or tails if so say yes if not say no, 
if you said no i will exit the code?"""
)

if Q1 == "yes": # you need to check equality with `==` between values/variable containing value
    Q2 = input( # Indentation should be consistent across the file as python is sensitive to indent
        """what word(s) or numbers(s) would 
    you like to use in your random 
    word or number picker?(also no 
    spaces in between numbers.)"""
    )
    y = random.choice(list(Q2))
    print(y)
elif Q1 == "no":
    print("bye")
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72792155

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档