Posts: 8
Threads: 4
Joined: Mar 2019
Mar-27-2019, 08:18 PM
(This post was last modified: Mar-27-2019, 08:19 PM by sunhyunshine.)
Hello everyone, i am new to python and i have homework assignment.
My python ver. is 3.6.6
#The user is asked to enter the title and year of issue of the book (this information is entered in two lines:
#Book_name <ENTER>,
#Book_Expansion_Years <ENTER>).
#Invalid entry of a name or book publishing year, this information is requested to be re-entered.
# Entry is terminated when the user enters the "end" termination clause.
#
# 1) Finding the oldest (oldest) books and the latest (newest books) and presenting to the user.
#
# 2) When a user enters an interval (eg 2010-2015), books published during this period are printed.
I got stuck with : Invalid entry of a name or book publishing year, this information is requested to be re-entered.
This is what i got so far, there might be mistakes as it is my first time programming.
book_name = [] # list of book titles
book_publishing_years = [] # list of book publishing year
a = "end"
b = "year"
c = 1
while (True):
b = input ("Enter the title of the book:")
if (b == a):
break
while (b is not b.isspace ()):
print ("Invalid book title")
b = input ("Repeat entry:") #this is where the while cycle repeats it's self and i don't know what to do next because the if is not being done?
if (b.isspace ()):
continue Thanks for any help.
Posts: 4,220
Threads: 97
Joined: Sep 2016
I don't understand the condition you are trying to get at. That is, what are you counting as an invalid title entry? If only black input is invalid, then you want if b == '': or better yet if not b.strip(): . If not, please explain in English what is considered an invalid title.
Note that you don't need parentheses around conditions ( if b == a: works fine). Also single letter variable names lead to confusing code. Use names that are more descriptive, like on the first two lines.
Posts: 8
Threads: 4
Joined: Mar 2019
(Mar-27-2019, 08:45 PM)ichabod801 Wrote: I don't understand the condition you are trying to get at. That is, what are you counting as an invalid title entry? If only black input is invalid, then you want if b == '': or better yet if not b.strip(): . If not, please explain in English what is considered an invalid title.
Note that you don't need parentheses around conditions (if b == a: works fine). Also single letter variable names lead to confusing code. Use names that are more descriptive, like on the first two lines.
Thank you for you response, in my case, invalid title entry would be a book title that doesn't have blank spaces, as if the book title can't be a one word.
Posts: 4,220
Threads: 97
Joined: Sep 2016
Okay. One way to do that is if ' ' not in title: . However, ' ' would pass that test. If you've used the split method of strings, which with no parameters splits a string into a list at white space, you could use if len(title.split()) > 1: , that would tell you there are at least two "words" in the title.
I'm assuming you got that specification from your teacher, but I would take issue with it. No 1984? No Cryptonomicon? No Annihilation? No Hamlet? No Frankenstein? No Dune? No Antigone?
Posts: 8
Threads: 4
Joined: Mar 2019
(Mar-27-2019, 09:51 PM)ichabod801 Wrote: Okay. One way to do that is if ' ' not in title: . However, ' ' would pass that test. If you've used the split method of strings, which with no parameters splits a string into a list at white space, you could use if len(title.split()) > 1: , that would tell you there are at least two "words" in the title.
I'm assuming you got that specification from your teacher, but I would take issue with it. No 1984? No Cryptonomicon? No Annihilation? No Hamlet? No Frankenstein? No Dune? No Antigone?
To be honest, i made that rule myself, because i couldn't think of anything else, i also tried making the rule, that the book title has to be with the first capital letter, but it didn't work. However, the problem is that the second while cycle doesn't move on and is just repeating itself. So basically, i just don't know what to do to make it work .
Posts: 4,220
Threads: 97
Joined: Sep 2016
Well, I wrote them as if conditions, but if you use the ones I gave you as while conditions, they should work on line 13. If they're not working, post the revised code and tell us exactly what is going wrong.
Posts: 8
Threads: 4
Joined: Mar 2019
(Mar-27-2019, 10:00 PM)ichabod801 Wrote: Well, I wrote them as if conditions, but if you use the ones I gave you as while conditions, they should work on line 13. If they're not working, post the revised code and tell us exactly what is going wrong.
Hello, i got some help and changed everything entirely, now i just need to know, how to repeat the while cycle and sort everything into the lists so i could use that information later?
the new code:
book_name = [] # list of book titles
book_year = [] # list of book publishing year
a = "end"
b = "year"
a = input ("enter the title of the book:")
while True:
while True: #now i need to repeat this cycle and sort everything into the lists again and again until the "end" word
a1 = a.capitalize ()
if a! = a1:
print ("Bad book title")
a = input ("Repeat :")
else:
book_name.append (a)
break
b = input ("Enter book year:")
while True:
b = int (b)
if b> 2019:
print ("Bad Year")
b = input ("Repeat book year:")
else:
book_year.append (b)
break
if a == "end":
break
Posts: 4,220
Threads: 97
Joined: Sep 2016
Well, it looks like you are putting it into the lists. I think if you put line 7 after line 9 (and indented), and move lines 27 and 28 after that, you should be good to go.
Posts: 8
Threads: 4
Joined: Mar 2019
(Mar-28-2019, 05:42 PM)ichabod801 Wrote: Well, it looks like you are putting it into the lists. I think if you put line 7 after line 9 (and indented), and move lines 27 and 28 after that, you should be good to go.
Thank you so much, it worked!!
|