Basic python. Quick question regarding calling a function [duplicate]

2024/10/11 9:29:30

I've got a basic problem in python, and I would be glad for some help :-)

I have two functions. One that convert a text file to a dictionary. And one that splits a sentence into separate words:

(This is the functiondoc.txt)

def autoparts():list_of_parts= open('list_of_parts.txt', 'r')for line in list_of_parts:k, v= line.split()list1.append(k)list2.append(v)dictionary = dict(zip(k, v))def splittext(text):words = text.split()print words

Now I want to make a program that uses these two functions.

(this is the program.txt)

from functiondoc import *# A and B are keys in the dict. The values are 'rear_bumper' 'back_seat'
text = 'A B'    # Input# Splits the input into separate strings.
input_ = split_line(text)

Here's the part I cant get right. I need to use the autoparts function to output the values (rear_bumper back_seat), but I'm not sure how to call that function so it does that. I don't think it's that hard. But I can't figure it out...

Kind Regards,

Th

Answer

Some quick points:

  • You should not name Python source files ".txt", you should use ".py".
  • Your indents look wrong, but that might just be Stack Overflow.
  • You need to call the autoparts() function to set up the dictionary.
  • The autoparts() function should probably return the dictionary, to make it usable by other code.
  • When opening a text file, you should use the t mode specifier. On some platforms, the lower-level I/O code must know that it is reading text, so you need to tell it.
https://en.xdnf.cn/q/118342.html

Related Q&A

Obtain the duration of a mp4 file [duplicate]

This question already has answers here:How to get the duration of a video in Python?(15 answers)Closed 10 years ago.I need to know the duration of a mp4 file with python 3.3. I search and try to do th…

Matplotlib.pyplot - Deactivate axes in figure. /Axis of figure overlap with axes of subplot

%load_ext autoreload %autoreload 2 %matplotlib inlineimport numpy as np import datetime as dt import pickle import pandas as pd import datetime from datetime import timedelta, date from datetime impor…

How to generate the captcha to train with Python

I would like to use deep learning program for recognizing the captcha using keras with python.But the big challenge is to generate massive captcha to train. I want to solve a captcha like thisHow can …

Convert PNG to a binary (base 2) string in Python

I Basically want to read a png file and convert it into binary(base 2) and store the converted base 2 value in a string. Ive tried so many things, but all of them are showing some error

Turbodbc installation on Windows 10

I tried installing turbodbc and it gives me the following error and not sure whats wrong here.My python version is 3.7My command line output from Windows 10 Pro. C:\Users\marunachalam\Downloads>pip …

how to convert a text into tuples in a list in python

I am a beginner in python and desperately need someones help.I am trying to convert a text into tuples in a list. The original text was already tokenized and each pos was tagged as below:The/DT Fulton/…

Trying to call a function within class but its not working [duplicate]

This question already has answers here:TypeError: attack() missing 1 required positional argument: self(2 answers)Closed 3 years ago.I am trying to call a function but its not working. here is the code…

Tkinter Label not showing Int variable

Im trying to make a simple RPG where as you collect gold it will be showed in a label, but it doesnt!! Here is the code:def start():Inv=Tk()gold = IntVar(value=78)EtkI2=Label(Inv, textvariable=gold).pa…

How to refer a certain place in a line in Python

I have this little piece of code:g = open("spheretop1.stl", "r") m = open("morelinestop1.gcode", "w") searchlines = g.readlines() file = "" for i, line…

List comprehension output is None [duplicate]

This question already has answers here:Python list comprehension: list sub-items without duplicates(6 answers)Closed 8 years ago.Im new to python and I wanted to try to use list comprehension but outco…