Hey Guys,

Firstly like to say this is a great forum and has helped me a great deal in regards to learling a bit about python!

But I have two programs which I require help with if possible :(

Program 1 - To search a text file for a user inputted word and display the lines in which that word occurs..

file_name = open ('test.txt', "r") 
f = file_name
line = f.readline() 
find_word = raw_input("Enter word to search here:")
if find_word in file_name:
    print f.readline(find_word)
f.close()`

Program 2 - *To calculate entry cost to swimming pool, I'm struggling with trying to make an if statement which will result in if there is 2 adults and 3 children then the entrance cost is $15 and not sure how to deal with invaild input? *

(This is the exact specification)

"Develop a program to calculate the entrance fee to a swimming pool, given the following pricing scheme:
adults £5, concessions £3, children under 16 £2, family groups (up to two adults and 3 children) £15.
Your system should calculate the cheapest fee for a given group and** should behave in a friendly manner
if invalid input is entered."**

while loop == 1:

    print "Welcome"

    print "What would you like to do?:"
    print " "
    print "1) Calculate entrance cost"

    print "2) Leave swimming centre"
    print " "

    choice = int(raw_input("Choose your option: ").strip())
    if choice == 1:
        add1 = input("Adults: ")
        add2 = input("Concessions: ")
        add3 = input("Children: ")
        print add1, "+", add2, "+", add3, "answer=", add1 *5 + add2 *3 + add3 *2   
    elif choice == 2:
        loop = 0

If anyone is out there that can help me it will mean a great deal and I will be very greateful!!

Recommended Answers

All 7 Replies

Hint, in program 1 you need to read all the lines. Right now you are only reading one line.

Thanks mate, have changed it to 'readlines' however still no luck?

Generally the lines are read and tested with something like this:

for line in open ('test.txt', "r"):
    print line  # test

Have changed it to that type of format, the program is running and asks which word to search but once inserted it doesn't actually 'print' anything..

Ok.. In regards to program one, I now have it working however it asks me the same question 4 times!?

for line in open("test.txt"):
 if raw_input("Enter word here: " ) in line:
   print line

Ignore last post, still stuck with both programs.

Program 1 now sorted with this code..

with open("G:/test.txt") as f:
   final_word=raw_input("Enter word here:")
   for line in f:                
       if final_word in line:    
           print line.strip() 

Any help with program two please?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.