Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python trouble #1
#1
Hello there!
I am a complete beginner when it comes in Python...
So..I have a .txt file which contains data of numerous mountains lin this form:
name1 height1 location1 (height1>height2>height3 etc..)
name2 height2 location2
name3 height3 location3

I am asked to create a program which takes as an input a name (of a mountain) from the user and also prints the data that are associated with it.For example if the input is name 2 then the program should print:
The name2 mountain has a height of height 2 is located in location2 and it is (height1-height2) lower than the mountain name1

My progress so far (If one could actually call it progress):
def f():
   x = input('For which mountain do you want to learn ? :')
   with open ( 'mountain3.py' ,'r',encoding = 'utf-8') as f :
     vouna3= f.read
     f = f.split(',')
     for z in f:
       y =  z.split(',',maxsplit = 4)
       z[0] = x
       z[1] = height
       z[2] = location (I have been working for these 10 lines for more than 3 hours :/)
One of the problems that i have is how can the program search for the namei (i = 1,2,3,4,5....) mountain in the .txt file and locate it...I am basically trying to create a list with sublists (one sublist is ['name1' ,'height1', 'location']1data of the mountains and if the first element (name of the mountain) of a sublist is x then it also print the rest data...Could somebody provide me with an insight or help me create the program in another way?

Ty Smile
Yoriz write Nov-17-2021, 06:23 AM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
Most of us are skittish about clicking on a posted link. Best if you post the code here. Use the BBC tags for code (looks like a blue and yellow icon on the toolbar)
Reply
#3
f is your file. vouna3 is the data from your file, but you do nothing with that. You then try to split the file.

Let's get you on the right path. For one thing, = assigns the value on the right to the variable on the left. So, z[0]=x would assign the value of x, which you don't have, to the 0 element in the array z, which is where you think you have the name. Not what you want to do.

Look at this and ask questions.
def f():
   x = input('For which mountain do you want to learn ? :')
   with open ( 'mountain3.py' ,'r',encoding = 'utf-8') as f :
     
     
     for z in f:
       y =  z.split(',',maxsplit = 4)
       name = y[0]
       height = y[1]
       location = y[2]
Note, I left it as a function like you had, though I see no reason for it to be a function. As a function you will need a line to actually call the function for it to do anything.
Reply
#4
Um when i paste the code you mentioned and run it I get this :
FileNotFoundError: [Errno 2] No such file or directory: 'mountain3.py'
...But I have the file named 'mountain3.py' in my computer

An idea would be to create a dictionary with keys the names of the mountains and somehow connect that with what i have as an input..
I also need that height difference between the mountain i have as input and the one that is immediately higher after it...

Some 'progress':
x = input('For which mountain do you want to learn ? :')
with open ( 'mountain3.py' ,'r',encoding = 'utf-8') as f :            
     for z in f:
       y =  z.split(',',maxsplit = 4)
       name = y[0]
       height = y[1]
       location = y[2]
       c = dict(c.key = name)
       print(c[x] , x)
       a = list(tuple(c.values)))
       for d in a:
           dif = 0
           for c in d:
               if c.isdigit:
                   dif = r - c #where r is the height of the mountain that is immediately higher than x....
Reply
#5
I missed that you opened mountain3.py as your data file in line 3. The .py extension means a python program and would be a bad name for a text file. You need to change that line to point to your text file containing the data.
So first thing you need to do is to show how you can read the data. Take what you have, change the name of the file you are opening to the proper name, print the values of name, height, and location as you read them. Once you can do that, you need to change it so you store the values in memory, likely a group of 3 lists. Then loop through the name list to find the name entered by the user. Take that index and print out the location, height, and height[index-1] and you will be done.
But 1 step at a time. You need to read the data. Then read it into your storage. I suggest lists over a dictionary as you have 3 pieces of information, not 2.
Reply
#6
Excuse me for my ignorance, but I renamed the file with the mountains data to mountain3.txt,also changed the name of the file in the program but I am still getting :
 FileNotFoundError: [Errno 2] No such file or directory: 'mountain3.txt'
UPDATE: It's ok now with
 
with open ('mountain3.txt' ,'r',encoding = 'utf-8') as f :            
     for z in f:
       y =  z.split(',',maxsplit = 4)
       name = y[0]
       height = y[1]
       location = y[2]
       print(name,height,location)            
but the program doesn't do something

update No2
 
x = input('For which mountain do you want to learn ? :')
with open ('mountain3.txt' ,'r',encoding = 'utf-8') as f :            
     for z in f:
       y =  z.split(',',maxsplit = 4)
       name = y[0]
       height = y[1]
       location = y[2]
       b = list(name)
       c = list(height)
       d = list(location)
       for i in range(len(b)+1):
           if x in d:
              print(name[i],height[i],location[i])  #still does not do something
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trouble with edX Python Final sarah_mb_sues 11 13,833 Jun-19-2018, 10:36 AM
Last Post: cryomick

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020