Python Forum
How to make it so whatever I input into a script gets outputted on a different file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to make it so whatever I input into a script gets outputted on a different file
#1
I am a newbie so please do not judge me, this is not the script its just an example of what it'd be like but I'd just like to get the understanding
n=input('Name: ')
a=int(input('a: '))
b=int(input('b:' ))
c=int(input('c: ' ))
avg=(a+b+c)/3
print(avg)
if avg>=19:
    print('distinction')
basically I just want that input to be saved into a different file instead of just doing the output once when running the program(I know there are programs specifically made for this eg Excel I just thought it would make a great project :) Thanks
deanhystad write Nov-22-2024, 10:19 PM:
Please post all code, output and errors (it it's 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
This was made on just notepad which is why I kinda messed up the print(avg) and also forgot the intends the actual code works fine C:
Reply
#3
I don't understand this:
Quote: I just want that input to be saved into a different file instead of just doing the output once when running the program
Do you mean that you want to read input from a file and perform the calculation multiple times? Maybe having a file that looks like this:
Output:
3,6,9 12,15,18 21,24,27
And he output something like:
Output:
average(3,6,9) = 6.0 average(12,15,18) = 15.0 average(21,24,27) = 24.0, extinction
That could be done like this:
with open("test.txt", "r") as file:
    for line in file.readlines():
        numbers = list(map(int, line.split(",")))
        average = sum(numbers) / len(numbers)
        suffix = ", extinction" if average >= 19 else ""
        print(f"average({line.strip()}) = {average}{suffix}")
But there are better tools for this. Pandas can read csv files and do calculations.
import pandas as pd

df = pd.read_csv("test.txt", names=["a", "b", "c"])
df["average"] = df.iloc[:, :].mean(axis=1)
df["extinction"] = df.average >= 19
print(df)
Output:
Output:
a b c average extinction 0 3 6 9 6.0 False 1 12 15 18 15.0 False 2 21 24 27 24.0 True
Reply
#4
damn thanks that seems nice but really I got no idea what most of those do ;-; you seem to know your stuff so any tips? do you just memorize everything? Do you just get the hang of it doing projects and stuff over time? How long did it take u to get to this level :O sorry if I sound weird I'm just new and it lowkey makes me wanna quit whenever I see this confusing ah stuff. It's like Greek to me, well except I'm Greek Angel Shifty
Reply
#5
df.iloc[:, :] is a pandas command. To understand what it does, read the pandas iloc documentation. Pandas documentation is very good.

To find out if iloc is the right tool to use for a problem, use google. There are a lot of pandas users, so any question you have has been asked and answered. When I wrote my example I googled "pandas row average". I don't use pandas enough to remember everything about it, and for what I do, loc and iloc are not something I often use.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Make entire script run again every 45 mo NDillard 0 910 Jan-23-2024, 09:40 PM
Last Post: NDillard
  Trying to make a board with turtle, nothing happens when running script Quascia 3 1,750 Nov-01-2023, 03:11 PM
Last Post: deanhystad
  how to make sure an input is from the wanted type astral_travel 16 5,079 Oct-31-2022, 04:11 PM
Last Post: astral_travel
  Make console show after script was built with Pyinstaller --NOCONSOLE? H84Gabor 0 1,827 May-05-2022, 12:32 PM
Last Post: H84Gabor
  Reading an Input File DaveG 1 1,868 Mar-27-2022, 02:08 AM
Last Post: deanhystad
  Make my py script work only on 1 compter tomtom 14 6,304 Feb-20-2022, 06:19 PM
Last Post: DPaul
  Controlling what get outputted to stdout when running external commands Daring_T 4 3,649 Jan-30-2022, 05:40 PM
Last Post: bowlofred
  Possible to execute a python script before log off/shutdown with input commands? Kaltex 1 3,132 May-18-2021, 06:31 AM
Last Post: Skaperen
  How to make a test data file for the full length of definition? MDRI 6 4,866 Apr-16-2021, 01:47 AM
Last Post: MDRI
  How to make python run other file? Adrian_L 4 3,310 Apr-06-2021, 06:39 PM
Last Post: ndc85430

Forum Jump:

User Panel Messages

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