Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Python Beginner loop help!

erestum

New Coder
Hi! I am just starting out and am trying to write a dichotomous key to identify minerals. I have a couple questions that I can't figure out...

1) After line 10, how can I get the program to loop them back to the question on line 4?
2) In general how do I get the program to re-ask a question if the appropriate answer isn't there
3) Same kind of loop problem, how can I get it at the end to entirely look back to question on line 11?
4) General feedback, can I make it look better?
[CODE lang="python" title="Key" highlight="10,52"]name = input("Hi! What is your name? ")
print ("Hello " + name + ", nice to meet you. ")

start = input("Do you want to identify a mineral? (y/n)")
if start.lower().strip() == " y":
print ("Great," + name + " ,let's get started")
elif start.lower().strip() == " n":
print ("Ok, " + name + " ,come back when you have a mineral to identify")
else:
print("Try again! ")
questionone = input("Ok, first question, is your mineral metallic or non-metallic?")
if questionone.lower().strip() == "metallic":
mhardness = int(input("Metallic,ok, what is the hardess of your mineral? "))
if mhardness > 4:
print ("A hardness over 4 means the mineral is considered hard. We are narrowing this down! ")
mcolor = input("Hard,ok, what color is your mineral? ")
if mcolor.lower().strip() == "gold":
mcolor = input("Gold, your mineral is pyrite! ")
if mcolor.lower().strip() == "black":
mcolor = input("Black,your mineral is magnetite! ")

if mhardness < 4:
print ("A hardness under 4 means the mineral is considered soft. We are narrowing this down! ")
mcolor = input("Soft,ok, what color is your mineral? ")
if mcolor.lower().strip() == "silver":
mcolor = input("Silver,your mineral is galena! ")
if mcolor.lower().strip() == "gray":
mcolor = input("Gray,your mineral is graphite! ")


elif questionone.lower().strip() == "non-metallic":
nhardness = int(input("Non-metallic,ok, what is the hardess of your mineral? "))
if nhardness > 4:
print ("A hardness over 4 means the mineral is considered hard. We are narrowing this down! ")
print ("A hardness over 4 means the mineral is considered hard. We are narrowing this down! ")
ncolor = input("Hard,ok, what color is your mineral? ")
if ncolor.lower().strip() == "white":
ncolor = input("White, your mineral is feldspar! ")
if ncolor.lower().strip() == "red":
ncolor = input("Red,your mineral is garnet! ")
if nhardness < 4:
print ("A hardness under 4 means the mineral is considered soft. We are narrowing this down! ")
ncolor = input("Soft,ok, what color is your mineral? ")
if ncolor.lower().strip() == "white":
ncolor = input("White,your mineral is calcite! ")
if ncolor.lower().strip() == "yellow":
ncolor = input("Yellow,your mineral is sulfur! ")
answer = input("Do you want to indentify another mineral (y/n) ?")
if answer.lower().strip() == " y":
print ("Great," + name + " ,let's get started")
elif answer.lower().strip() == " n":
print ("Ok, " + name + " ,come back when you have a mineral to identify")[/CODE]
 
Hello erestum!
You can repeat a set of instructions using the loop 'while':
Python:
appropriateAnswer = False

# Repeats the instructions as long as 'appropriateAnswer' does not equal 'True'.
while appropriateAnswer != True
    #instructions...

I do not really code in Python, so about the general feedback, I will let that to others if they have something to say about that ;).
 
Like LTomy said, while loops are probably the best way to loop back and repeat instructions. However, you could also use 'goto' labels, but using too many of them may end up in some messy code. To use goto labels you have to run the command 'pip install goto-statement' in your command prompt. Here is an example of a goto function:
[CODE lang="python" title="Goto Labels" highlight="1"]from goto import with_goto
print("Hi")
goto .end
print("This shouldn't show!")
label .end
print("This should show!")
[/CODE]
 
Like LTomy said, while loops are probably the best way to loop back and repeat instructions. However, you could also use 'goto' labels, but using too many of them may end up in some messy code. To use goto labels you have to run the command 'pip install goto-statement' in your command prompt. Here is an example of a goto function:
[CODE lang="python" title="Goto Labels" highlight="1"]from goto import with_goto
print("Hi")
goto .end
print("This shouldn't show!")
label .end
print("This should show!")
[/CODE]
That would also work, but I think 'goto' statements make the code harder to understand and many people believe they should be avoided.
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom