Python Forum
Need help with iteration homework
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help with iteration homework
#3
On the homework board, I usually wouldn't do a full rewrite. I'm going to make an exception this time.

On line 1, we have two issues:

1. The function call is not closed. For every open parenthesis, you need a closed parenthesis; benefit of the doubt, it's a typo.
2. As ichabod asked, why are we changing the input to an integer for this? Based on the rest of the code, we want to compare the input to nucleic acids, which are letters. Now, we could encode the acids as integers for the comparison testing; but I believe in KISS: keep it simple, stupid (not a directed insult, just an adage).

So, let's rewrite line 1 and rename the variable to something meaningful:

user_input = input("Enter a single character")
As stated above, the indenting is just too much. The if statement is nested under anything so it should not be indented. Plus, we need to compare our established variable against the known acids. Remember to use "==" for equality comparisons. So, our comparisons need to be in the form of:

user_input == "A"
This way, the value of the variable will be compared to the value on the right. Because input() returns a string, our variable is a string and the quotes indicate that the value on the right is also a string; no problems.

As stated above, Python uses "elif" instead of "else if". Plus, the final line should be an "else" clause at the end. So, your if statement should look like:

if user_input == "A":
    print("Valid DNA")
elif user_input == "C":
    print("Valid DNA")
elif user_input == "G":
    print("Valid DNA")
elif user_input == "T":
    print("Valid DNA")
else: print("Invalid DNA")
Notice that there's no "break" keyword. "Break" is only used for loops. Now, you could have a loop surrounding this snippet (which would be a good pattern for user input). Without a loop, "break" will cause an error.

Now, we can make an improvement to simplify it. We repeat the line "print('Valid DNA')" four times. We can reduce our tests and use that line only once... And that's where I'll cut it. You can work on simplification.
Reply


Messages In This Thread
Need help with iteration homework - by Dendro - Oct-18-2018, 12:35 AM
RE: Need help with iteration homework - by stullis - Oct-18-2018, 02:53 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Homework (counting iteration issue) Cardinal07 10 5,697 Feb-26-2018, 04:45 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020