I have some code and i want to go back into a certain line however i am not sure how i can achive such things.

#SnakeFactBook v.1

#Here is the begining of the book
print("Hello.\nWelcome to the Bush Viper Fact Book.")

#Ask for name
name = input("What is your name?")
print("Ok, why hello,",name)

#Tells you what this is
print("There are six pages to the book.",name)
print("Which one would you like to choose?")
print("Page 1=1 ")
print("Page 2= 2")
print("Page 3= 3")
print("Page 4= 4")
print("Page 5= 5")
print("Page 6= 6")
#Asks you for the page choice
pagechoice = input("Please choose one:")

#Now it will determine which page you choose.

if pagechoice == '1':
    print(""" This is page one """)
elif pagechoice == '2':
    print(""" This is page two """)
elif pagechoice == '3':
    print(""" This is page three """)
elif pagechoice == '4':
    print(""" This is page four """)
elif pagechoice == '5':
    print(""" This is page five """)
elif pagechoice == '6':
    print(""" This is page six """)
else:
    print("Sorry, but what you have typed in is invalid,\nPlease try again.")

Just saying i am a noob at python so please don't spam.

Recommended Answers

All 4 Replies

Use a function http://www.tutorialspoint.com/python/python_functions.htm and call it as many times as you like

import time

def print_something(ctr):
    choices = ["zero", "one", "two", "three", "four",
               "five", "six"]
    if ctr < len(choices):
        print(" This is page", choices[ctr])
    else:
        print("Sorry, but what you have typed in is invalid,\nPlease try again.")

ctr=1
print_something(ctr)
time.sleep(1.0)
ctr = 6
print_something(ctr)
time.sleep(1.0)
ctr = 10
print_something(ctr)

Woooee's advice is spot on, as far as it goes. However, if you are trying to repeat something without knowing how many times to repeat it, what you would want is a while: loop.

pagechoice = '1'
while pagechoice != '0':
    pagechoice = input("Please choose one:")

    #Now it will determine which page you choose.

    if pagechoice == '1':
        print(""" This is page one """)
    elif pagechoice == '2':
        print(""" This is page two """)
    elif pagechoice == '3':
        print(""" This is page three """)
    elif pagechoice == '4':
        print(""" This is page four """)
    elif pagechoice == '5':
        print(""" This is page five """)
    elif pagechoice == '6':
        print(""" This is page six """)
    else:
        print("Sorry, but what you have typed in is invalid,\nPlease try again.")

this will keep asking for a page number until the user enters a zero. On the gripping hand, if you have something where you want to repeat a fixed number of times - say, one time for each member of a list - you can use a for: loop:

for x in ['a', 'd', 'm', 'e', 'h']:
    print(x)

With a for: loop, it will set the index variable (in this case x) to the next member of the list until it reaches the end of the list. So on the first pass, x would equal 'a', on the second, x would equal 'd', and so forth.

wooee could you simplify that as to do just 2.
i.e Call that function and have 1 correct 'yes' responce and an else 'no responce'.

As i said i am still not as confident at python but still trying.

Well, we could take the existing function and simplify it like this, if you like:

def print_something(ctr):
    if ctr == '1':
        print("This is a valid entry.")
    else:
        print("Sorry, but what you have typed in is invalid,\nPlease try again.")

But I am not certain what that would clarify for you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.