繁体   English   中英

else 上的语法错误:代码中的语句 + idk 为什么它一直说我的标题不好

[英]Syntax error on else: statement in code + idk why it keeps saying I have a bad title

所以,我的问题是我需要把 mondayAssign 放在我有它的地方,因为它似乎只会在其他地方问这个问题,我也不想要它,所以我必须这样做,我在 elif 上有一个错误:任何一个想帮忙吗?

__import__("replit").clear()

whatReturn = "monday"
f = open('hold.txt',  'r')
g = f.seek(0)
h = f.readlines()
for line in h:
  pass

def monday():
 if whatReturn in line:
   mondayAssign = input("Assign a task for monday: ")
 else:
   print("error has occured.")
monday()
def mondayy():
 if whatReturn in line:
   mondayAdd = input("Anything else you would like to add, 'yes' or 'no': ")
   mondayAdd == 'yes'
   mondayDoubleAssign = input("Assign a task for monday: ")
 elif:
   print("Error!!!")
 else:
   mondayAssign = input("Assign a task for monday: ")
#the last else is just a place to store the mondayAssign cause it appears I cant put it anywhere else without it just printing itself and I don't want to ask same question twice
mondayy()       
print(mondayAssign, mondayDoubleAssign)

这很简单,您使用无条件的elif构造。 添加一些条件或将elif替换为else 如果要处理异常,则需要使用try except构造。 这是使用“else”的变体:

__import__("replit").clear()

whatReturn = "monday"
f = open('hold.txt',  'r')
g = f.seek(0)
h = f.readlines()
for line in h:
  pass # This is extra code

def monday(h):
    for line in h:
        if whatReturn in line:
            mondayAssign = input("Assign a task for monday: ") # to use "line" you must first define it
        else:
            print("error has occured.")
monday(h)
def mondayy(h):
    for line in h:
        if whatReturn in line:
            mondayAdd = input("Anything else you would like to add, 'yes' or 'no': ")
            mondayAdd == 'yes' #it's not very clear why there is a comparison
            mondayDoubleAssign = input("Assign a task for monday: ")
        elif some_condition:
            print("Error!!!")
        else:
            mondayAssign = input("Assign a task for monday: ")
#the last else is just a place to store the mondayAssign cause it appears I             cant put it anywhere else without it just printing itself and I don't want to ask same question twice
mondayy(h)       
print(mondayAssign, mondayDoubleAssign)

如果您总体上查看代码,我认为您应该对循环和条件语句有更好的理解,因为您似乎对它们有问题。 不要一下子抓住所有东西:)最好先详细研究每个主题,然后尝试将它们结合起来。 顺便说一句,这也适用于函数。 这里有一点关于使用它的潦草:

#lets begin from loops
#simple usage:
for some_variable in some_sequence:
    print(some_variable) # some variable access only in loop it means that you can use it only in loop body(4 spaces)
#now briefly about conditional operators:
if some_statement:
    print("something")
elif some_other_statement:
    print("do something else") # it means optional condition
else:
    print("do something different in any way that does not fit the above")
#again, you need to remember about the scope, that is, something will work inside the condition only if it is written after 4 spaces
#now briefly about functions:
#a function is an algorithm that we have named by some name and, as a rule, we use repeatedly. In parentheses, we pass the arguments to the function, that is, external data that needs to be used inside the algorithm.
def simple_func(arg_one, arg_two):
    print(arg_one, arg_two)
simple_variable_one = "hello"
simple_variable_two = "world!"
simple_func(simple_variable_one, simple_variable_two) #output: hello world

祝你学习Python好运,希望你成功!

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM