Python Forum
Need help with iteration homework
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help with iteration homework
#1
Hi guys, i'm really bad at python and could really use help solving this homework
[Image: 2732bfb979.png]

This is my attempt
[Image: 40593e8b95.png]

I know my code is probably terribly wrong, but i don't know where to begin to start
How do i create code where i prompt the user to enter "a single character" and respond whether it's valid or invalid?
I could really appreciate help, thank you
Reply
#2
Please use python tags when posting code. See the BBCode link in my signature below for instructions.

First you need to fix your syntax and indentation errors. The if and else statements should all be at the same indentation level as the first input. There is no "else if" in Python, it is "elif". And you can't use break outside of a loop.

Next, if they are entering a character, why are converting it to an int? And note the missing close parentheses at the end of that line. Also, you store the value in n, but you are checking against int (a built-in function).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#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


Possibly Related Threads…
Thread Author Replies Views Last Post
  Homework (counting iteration issue) Cardinal07 10 5,587 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