I'm currently taking a beginner programming course, and we are using Zybooks. I'm working on a participation activity where I am tasked with completing an existing program to verify that the string contains a valid integer. The existing code cannot be changed.
Here is that code:
string = input()
valid = True
i = 0
while valid and i < len(string) :
# To be valid, the string can only contain digits
# and a sign character (+ or -) as the first character.
# Your code goes here
if valid :
print("valid integer")
else :
print("invalid integer")
Here is my code:
string = input()
valid = True
i = 0
while valid and i < len(string) :
# To be valid, the string can only contain digits
# and a sign character (+ or -) as the first character.
if not string[i].isdigit() :
valid = False
i += 1
if valid :
print("valid integer")
else :
print("invalid integer")
I am understanding that I need to first do an empty string check before I start code checking the integers and whether they have a " +/-." Then I would start increasing [i] inside the loop after checking for +/-.
I tried omitting the "if not string[i]..." portion and only leaving in the "valid = False," however that is still giving me an infinite error loop. Zybooks is tricky. I'm not sure if there is only supposed to be one line or code or more.
There are five inputs the code tests: -85, 78, x25, +2050, and 19-5. With my current code entry, I am only getting x25 (invalid integer) correct. With the others the output is incorrect, or I have an infinite loop issue.
I don't understand where my code is wrong. I suspect it's something with the "if not" string.
Thank you in advance for your help.
I'm currently taking a beginner programming course, and we are using Zybooks. I'm working on a participation activity where I am tasked with completing an existing program to verify that the string contains a valid integer. The existing code cannot be changed.
Here is that code:
string = input()
valid = True
i = 0
while valid and i < len(string) :
# To be valid, the string can only contain digits
# and a sign character (+ or -) as the first character.
# Your code goes here
if valid :
print("valid integer")
else :
print("invalid integer")
Here is my code:
string = input()
valid = True
i = 0
while valid and i < len(string) :
# To be valid, the string can only contain digits
# and a sign character (+ or -) as the first character.
if not string[i].isdigit() :
valid = False
i += 1
if valid :
print("valid integer")
else :
print("invalid integer")
I am understanding that I need to first do an empty string check before I start code checking the integers and whether they have a " +/-." Then I would start increasing [i] inside the loop after checking for +/-.
I tried omitting the "if not string[i]..." portion and only leaving in the "valid = False," however that is still giving me an infinite error loop. Zybooks is tricky. I'm not sure if there is only supposed to be one line or code or more.
There are five inputs the code tests: -85, 78, x25, +2050, and 19-5. With my current code entry, I am only getting x25 (invalid integer) correct. With the others the output is incorrect, or I have an infinite loop issue.
I don't understand where my code is wrong. I suspect it's something with the "if not" string.
Thank you in advance for your help.
Share Improve this question edited Mar 26 at 17:35 PS Ormenda asked Mar 26 at 4:40 PS OrmendaPS Ormenda 171 silver badge6 bronze badges 4 |4 Answers
Reset to default 0Given the constraint that the existing code cannot be modified then:
string = input()
valid = True
i = 0
while valid and i < len(string) :
try:
int(string)
except ValueError:
valid = False
break
if valid :
print("valid integer")
else :
print("invalid integer")
It's worth noting that the code that you're not supposed to modify is fundamentally flawed. What if the input string is empty? It would be considered as valid because the while loop would never be entered.
Maybe Zybooks isn't the best place to try to learn Python
Thank you very much for your feedback and help. I now know that this website is not for beginner Programmers. My brain is struggling to wrap around the coding concept. Thankfully, I have found some beginner Python forums and I will direct my questions there. Secondly, yes I agree that Zybooks is awful. We are literally unable to delete or change existing code as the program will not allow you too. It can be frustrating when asking for help or using tutoring as they often point out the errors.
Lastly, with your help I found the problem. Here is the corrected code with 5/5 tests passed.
string = input()
valid = True
i = 0
while valid and i < len(string) :
# To be valid, the string can only contain digits
# and a sign character (+ or -) as the first character.
if i == 0:
if not (string[i].isdigit() or string[i] in ['+','-']) :
valid = False
else :
if not string[i].isdigit() :
valid = False
i += 1
if valid :
print("valid integer")
else :
print("invalid integer")
First glance it would be good advice to increment your i outside of the if statement. Otherwise if one of the parts of your string is a digit, i won't get incremented which leads to the looping your experiencing.
Also to you need to check the first sign of your string for + or -. Note that you would normally not do that in the while loop, because it would be inefficient, however the assignment requires you to do it like that.
string = input()
valid = True
i = 0
while valid and i < len(string) :
# To be valid, the string can only contain digits
# and a sign character (+ or -) as the first character.
if len(string) == 1:
if not string[i].isdigit() :
valid = False
if i == 0:
if not string[i].isdigit() :
if not string[0] in ['+','-']:
valid = False
if i > 0:
if not string[i].isdigit() :
valid = False
i += 1
if valid :
print("valid integer")
else :
print("invalid integer")
The i += 1
bit is indented too far. It is part of the if block therefore it only increments i if the character is invalid. Revised code:
string = input()
valid = True
i = 0
while valid and i < len(string) :
# To be valid, the string can only contain digits
# and a sign character (+ or -) as the first character.
if not string[i].isdigit() :
valid = False
i += 1
if valid:
print("valid integer")
else :
print("invalid integer")
Hopefully this solves your problem.
valid = string[1:].isdigit() and string[0] in '+-'; break
– cards Commented Mar 26 at 11:56