How to skip lines of code in python

let's start from the top:

think=input ["Think of an animal. Type ready when you want to begin"]
think=think.upper[]
#FUR
if think=="READY" :
   fur=input ["Does it have fur?"] 
else :
   print ["I'll be waiting"]

if user enter anything else except "ready" for the first input which is stored in "think" then if condition will be false and your program straightly will go to else part so in the second part:

if fur=="YES" :
   legs=input ["Does it walk on four legs?"] :
elif fur=="NO" :
   reptile=input ["Is it a reptile?"]

it'll crash your program cause there is no variable called fur and you want to compare it with something.

for situation like this[waiting till user enter your expected input] it'll be better to use an infinite loop and when user entered your expecting input get out from that using break.

so you must change first part to:

think=input ["Think of an animal. Type ready when you want to begin"]
think=think.upper[]
#THINK
while True:
    if think=="READY" :
        fur=input ["Does it have fur?"]
        break
    else :
        print ["I'll be waiting"]

for other parts the exact thing as above may occur again[ for instance as you mentioned if user say "YES" to "Does it walk on four legs?" again you don't have any variable called reptile which you want to compare it with something else in next lines]

I suggest to use nested conditions:

#FUR
if fur=="YES" :
    legs=input ["Does it walk on four legs?"]
    #LEGS
    if legs=="YES" :
       pet=input ["Do people own it as a pet?"]
    elif legs=="NO" :
       marsupial=input["Is it a marsupial?"]
elif fur=="NO" :
    reptile=input ["Is it a reptile?"]
    #REPTILE
    if reptile=="YES" :
       shell=input ["Does it have a shell?"] 
    if reptile=="NO" :
       fly=input ["Can it fly?"]

also don't forget to:

1-clear the : in this part of your code

legs=input ["Does it walk on four legs?"] :

2-if you want to get a newline after asking something from user such as in first line, \n must be useful

think=input ["Think of an animal. Type ready when you want to begin\n"]

or even can use print for the string[cause print automatically will add a newline after each time you use it]:

print["Think of an animal. Type ready when you want to begin"]
think=input[]

In this article will see how to skip a line in a file in Python. There are multiple ways to do that. In this post, we will discuss two approaches.

1. Using the readlines[] method

The readlines[] method reads a file and returns a list. Here, each item of a list contains a line of the file, i.e., list[0] will have the first line, list[1] the second line, and so on.

Since it is a list, we can iterate over it. When the current line number is equal to the line number that we want to skip, we omit that line. Otherwise, we consider it.

Consider the following example in which we print all lines, except the one that we want to skip.

def skipLine[f, skip]:
  lines = f.readlines[]
  skip = skip - 1 #index of the list starts from 0

  for line_no, line in enumerate[lines]:
    if line_no==skip:
      pass
    else:
      print[line, end=""]

Let’s try out the above code by skipping the first line of the sample.txt file.

sample.txt

This is a sample file.
Python is a very powerful programming language.
Let's see how to skip a line in Python.
It is very easy.
I love Python. It makes everything so fun.
try:
  f = open["sample.txt", "r"]
  skipLine[f, 1] 
finally:
  f.close[]

Output

Python is a very powerful programming language.
Let's see how to skip a line in Python.
It is very easy.
I love Python. It makes everything so fun.

Let’s now skip the 3rd line.

try:
  f = open["sample.txt", "r"]
  skipLine[f, 3] 
finally:
  f.close[]

Output

This is a sample file.
Python is a very powerful programming language.
It is very easy.
I love Python. It makes everything so fun.

If you pass a value that is greater than the total number of lines or less than 1, then nothing will happen.

2. Using the readlines[] method and List Slicing

Since the readlines[] method returns a list, we can perform slicing to skip a specific line. Consider the following example.

def skipLineSlicing[f, skip]:
  skip -= 1 #index of list starts from 0
  if skip 

Chủ Đề