最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - Complete the program to verify that a string contains a valid integer value. To be a valid integer the string can only

programmeradmin6浏览0评论

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
  • 1 You have enough hints above to resolve this specific question. You might want to try python tutor to debug such simple code by stepping through line-by-line and checking what happens. – user19077881 Commented Mar 26 at 5:02
  • 1 Do you consider, for example, superscript two to be a valid integer? "²".isdigit() == True but if you tried int("²") it would fail with a ValueError exception. – Adon Bilivit Commented Mar 26 at 8:02
  • what I don't understand as well: it's a valid int if it starts with + or -. however the check for that should by assignment only be done in the while loop. why? – Bending Rodriguez Commented Mar 26 at 10:01
  • as long not dealing with empty strings: valid = string[1:].isdigit() and string[0] in '+-'; break – cards Commented Mar 26 at 11:56
Add a comment  | 

4 Answers 4

Reset to default 0

Given 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.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论