r/pythonhelp • • Sep 17 '21

HOMEWORK Need help fixing this syntax error.

3 Upvotes

I’m getting a syntax error at the print part and I don’t know why. I started coding recently and I’m a noob. I would appreciate any help.

item1 = (input("Please enter the price of the first item : ")) item2 = (input("Please enter the price of the second item : ")) item3 = (input("Please enter the price of the third item : ")) item4 = (input("Please enter the price of the fourth item : ")) item5 = (input("Please enter the price of the fifth item : ")) total = item1 + item2 + item3 + item4 + item5 salesTax = 0.7 subTotal = (total + (total * salesTax) print = total, salesTax, subTotal


r/pythonhelp • • Aug 31 '21

HOMEWORK Help with Python exponential function

3 Upvotes

So, I started my coding adventure yesterday and started from "4 hours full course for beginners of python". I had problems understanding how below code works, it's exponential function and I can't really understand It.After defining a function, we make container called "result" where we store below list of numbers and I can't understand how its gonna make 2^3=8? How can list of numbers be multiplied by 2? Someone can help?


r/pythonhelp • • Aug 28 '21

Asing list_1 to list_2 and clear list_2 and also not clearing list_1

3 Upvotes

This have me crazy, when I clear list2 it also clears list1 because it is equal to it, but I need list1 to just save it and not get cleared when I clear list2.

list2 = ["2", "hello"]
list1 = list2
list2.clear()
print(list1)

I need the output to be ["2", "hello"] and not []

I hope it aint too stupid of a question.

Thanks.


r/pythonhelp • • Aug 28 '21

HOMEWORK i need help with a random generator

3 Upvotes

so i made a script

import random

print(random.randint(1,5))

but how do i make it so that if 1 print ()


r/pythonhelp • • Aug 03 '21

How do I print a custom error message without Python returning a traceback?

3 Upvotes

I have 2 programs that require a numerical input. One takes a float, the other takes an integer.

Program 1

Program 2

If they detect letters or symbols, it's supposed to return the error message "That wasn't a float/number!"
While it does return the error message, they also return Traceback (most recent call last): NameError: name 'Example' is not defined together with the error message.

How do I make it return ONLY the error message without the Traceback thing?


r/pythonhelp • • Aug 01 '21

how to remove uneseccary lines form a a string?

3 Upvotes

say i have a string that looks like this:

"hi hi hi

hi hi hi"

And i want to chop off the bottom row hi hi hi so it looks like just this:

"hi hi hi"


r/pythonhelp • • Jul 30 '21

HOMEWORK need help with python tax calculator

3 Upvotes

I'm new to python. Made a simple calculator for tax but want to add 12000 personal allowance in - so if the salary is <= 12,000 the 20% tax should no be deducted - also if the salary is 13,000 the 20% should be deducted from the 1000.

right now it works - but the 20% is deducted from everything

hours = input ("Number of Hours Per Week? ")

rate = input ("Your Hourly Rate? ")

wp = float(hours) * float(rate)

#wp is weekly pay

print ("weekly pay: ", wp)

#mp is monthly pay

mp = float(wp) * 4

print ("Monthly Pay: ", mp)

salary = float(mp) * 12

print ("Yearly Salary: ", salary)

pa = 12000

print ("Personal Allowance: ", pa)

quotient = 1 / 5

tax = float(salary) * float(quotient)

print ("20% tax: ", tax)

#sat means salary after tax

sat = float(salary) - float(tax)

print ("Salary after tax & PA: ", sat)

mat = float(sat) / 12

print ("Monthly Pay after tax & PA: " ,mat)


r/pythonhelp • • Jul 29 '21

My Script Runs in Pycharm but Will Not Run in Python

3 Upvotes

I wrote a piece of code and I have been testing it in Pycharm and it works exactly as intended, but when I go to run it through regular Python, it doesn't complete the code.

The program allows for the user to name and save a file to their device, when I run it in Python after it asks for the file name it will automatically close out without saving the file. It is not supposed to do this. It is supposed to save the file and after asking the user to repeat the program, iterate through the code again or then close the program.

Every other feature of the code works but it will not save the file and just crashes. Why is this? Do I need to change the code or something? And if not, is there another way to run this script? I tried doing it through the terminal as well but made no progress.

I will leave the part of the code that deals with the file saving below;

Ask_User_To_Save_Dataframe = input("Do you want to save your Dataframe, above, as a file to this computer? Yes or No: ")
if Ask_User_To_Save_Dataframe == str("yes") or Ask_User_To_Save_Dataframe == str("Yes"):
User_Names_Save_File = input("What do you want to name the file of the above Dataframe?: ")
f = open(User_Names_Save_File, "x+")
Turn_Dataframe_To_String = Drop_All_Na_From_Dataframe.to_string()
f.write(Turn_Dataframe_To_String)
f.close()


r/pythonhelp • • Jun 22 '21

How do I add a Python project to a webpage?

3 Upvotes

Hello all, I’m trying to add a Python project I finished a week ago to my portfolio webpage and am having a hard time doing so. The website will only accept ipynb files in terms of coding. I managed to get the file and post it, but when I try to play it it doesn’t work. What else can I do? Thanks for your help.


r/pythonhelp • • Jun 13 '21

Coding a discord bot on Repl.it , and came across this error, any ideas on how to fix it?

3 Upvotes

r/pythonhelp • • Jun 11 '21

HOMEWORK Unexpected Output (printing and returning a string space instead of a word)

3 Upvotes

I am doing an assignment where it requires me to create a code that takes a string sentence and returns and prints the longest word in the sentence. I am not allowed to use .Split() to separate each word, as the assignment is trying to get me to use iteration. However, the output I am getting is only a space and I do not know what I am doing wrong with my iteration.

Here is my code:

def longestWord(sentence):
    longerWord = ""
    currentWord = ""
    for count in sentence:
        count = 0
        if (sentence == " "):
            count += 1
        elif len(currentWord) > len(longerWord):
            longerWord = currentWord
            count += 1
    return longerWord
    print (longerWord)

r/pythonhelp • • May 14 '21

SOLVED Unpacking function return in list comprehension amidst other values

3 Upvotes

I've paraphrased a bit on the code (leaving out quotes or whatever) but you should definitely be able to understand what I'm getting at.

I have a function to manipulate a couple values but there are other values that need to remain the same. I've tried to restructure the function to ingest variable lengths of iterables but not having much luck.

software = {
    'software_1' = {
        'var1' = 'publisher',
        'var2' = 'product',
        'var3' = 'version'
    }
}

def func(publisher, product):
    publisher = publisher.replace(',', '')
    product = product.replace(',', '')
    return publisher, product

In my mind, it would just work like this but it seems you can't unpack at this level of the comprehension...

manipulated_software = [line_item['var3'], *func(line_item['var1'], line_item['var2']) for line_item in software]

And hoping for output like...

manipulated_software = [version, publisher, product]

As opposed to what I'm getting now which is more...

manipulated_software = [version, (publisher, product)]

r/pythonhelp • • May 09 '21

Why does professional code have a bunch of functions with naming convention __something__ ?

3 Upvotes

I am not an expert but I have done several things in python and never had to use those, what am I missing?


r/pythonhelp • • Apr 17 '21

Using Python How Do I send FFMPEG stderr and stdout to Multiple Files?

Thumbnail self.codinghelp
3 Upvotes

r/pythonhelp • • Apr 06 '21

HOMEWORK Hi everyone I need to write a code that plots graph for vertical height and time of the space craft took to land. The question and a sample code is shown below. I just need to complete the part where there are three dots given but I dont understand how to do it.

Thumbnail
gallery
3 Upvotes

r/pythonhelp • • Apr 05 '21

need help with python pandas, remove duplicates functionality used for any DataFrame's specific column(s), with a single exception:

3 Upvotes

Hello there,

I'm using python to read .csv files, and then manipulate it's Series and DataFrames.

Anyways, I'm trying to figure out how to implement a remove duplicates functionality for all the cells of one or more columns of a DataFrame read from a .csv file, in which inside the python script, you set what symbol you want the duplicates to be removed for in all the cells of the column(s) of the DataFrame, with an exception, do not remove duplicates of a symbol when that symbol is the only symbol of a cell, and it's by itself in that cell with no other values.

Example:

If you have columns A and B, and within both of the columns there's dashes( - minus symbol/character) being used, if those dashes are being used with other symbols, like for example: 852-541-897 or 4-12-2021 or bob-tom or -c, then remove those dashes leaving the other symbol(s) there, example continued, result data of cells after duplicate function is applied: 852541897 or 4122021 or bobtom or c, but, the only exception is when a single dash - is by itself in a cell with no other values, in that case do not remove that single - that's by itself in the cell with no other values. A single dash - by itself in a cell with no other values has special meaning for my use case, so I don't want those dash specific single dash - duplicates removed unless they're with other values.

note: i'm aware that when opening a .csv file in excel, that excel has a remove duplicates button, but it would also remove the single dash - duplicates that are by themselves in the cell without any other values, so i'm trying to write a simple python script to account for this exception, that gets applied to the entire DataFrame's column(s) cell values.

also, one last thing, i'm going to use pyinstaller to generate an executable from the python file, to double click on, so i'm also wondering, what python package has the functionality, so that when I double click the python executable it brings up the windows 10 file explore(which opens up in the path relative to the path(current working directory) where the python executable is double clicked in.), so that 1 or more .csv files can be selected all at once, so that the remove duplicates functionality knows what .csv files to be applied to.

I hope I explained this specific remove duplicates functionality clearly,

thanks in advance for the help!


r/pythonhelp • • Apr 01 '21

Help with getting return from Class

3 Upvotes

I am trying call a function inside a class, have it send another function a value and then return it. But the return variable is always None? Can someone help me? In the pinto() function below it prints the correct value (2), but when returned to the variable (v) below to print it prints None?

class Testy():

def sec(self):

ri = 2

self.pinto(ri)

def pinto(self, ri):

print(ri)

return(ri)

r = Testy()

v = r.sec()

print(v)


r/pythonhelp • • Mar 13 '21

Python script: Learning to Webscrape local businesses email and phone number from search engine

3 Upvotes

Hi,

I run a small local business that was unfortunately heavily hit by the covid-19 crisis through the last year. Trying to improve our situation by finding smarter ways of reaching out to potential business customers and recently discovered the concept of webscraping. I have started to learn a little python at home and am trying to write a webscraping script with the following properties:

  1. used in connection with search engines like google, google maps, bing, etc.

  2. finds all local businesses in a geographically defined area with their associated websites

  3. clicks on every business-website, looks for, and extracts the following business information: Business Name, website url, email, telephone number, address (here I noticed that there may be a challenge due to the fact that not everything is marked as such and may just be plain text, thus I believe that one might have to write something that looks for "some_characters"@"some_other_characters"."x_characters" to identify e.g. an email address hidden in the text of the website?)

  4. moves this information into a csv. file

As I am still fairly inexperienced and trying to learn from others by reading already existing scripts, I was wondering if anyone here knows about a publicly available python script that does something similar as described above that I could read through to understand more and perhaps customize. So excited to learn more about the world of programming and webscraping, thank you in advance for your help and time! :)

- Amy
P.s.: If you're just starting to learn webscraping in python like me I am very happy to recommend this tutorial as it helped me a lot with understanding the basics: https://oxylabs.io/blog/python-web-scraping / https://www.youtube.com/watch?v=mDveiNIpqyw


r/pythonhelp • • Mar 09 '21

SOLVED Very simple if input is ab then print cd problem

3 Upvotes

So I want to make a very simple program where I have 3 different text options. The user inputs a number and the program should then print the text that belongs to that number, basically.

Apple = 1 Tree = 2 Dog = 3

print(' ')

Number = input("Type in a number: ")

print(' ')

if Apple True:

print('Apples are good')

print(' ')

if Tree True:

print('Trees are good')

print(' ')

if Dog True:

print('Dogs are good')

print(' ')

This is the code I have so far but I am kinda stuck. I know this does not work but I don't know how to solve this myself. Could somebody please help me? Thanks in advance.


r/pythonhelp • • Mar 09 '21

SOLVED Need help with an "if" and "input" code

3 Upvotes

Hello

I am having a problem with this code I am trying to make for my friends, every time I run it it works fine, however when I input "a" it dosent print("Welcome User")

a = (14451444)

a = input("Insert Code: ")

if a == b:

print("Welcome User")

any help with this?


r/pythonhelp • • Mar 07 '21

Capitalization Python: noob question

3 Upvotes

Hi, thanks for ur help in advance.

I have a string "I like cats and dogs and monkeys".

And I have a function that changes it to this:

"i like cats

and dogs and

monkeys."

How do I get it so the first letter of each line is capitalized? thanks


r/pythonhelp • • Mar 07 '21

HOMEWORK I need to create a number guessing game with a score.

3 Upvotes
#Imports
import random
import time
import os

def Main():
    file = open("score.txt","r")
    score = int(file.read())
    file.close()
    print("your current score is" , score)
    
    time.sleep(1)
    
    sides = int(input("How many sides do you wish your dice to have(Difficulty): "))
    if sides == 1:
        sides = 2
    guess = int(input("What do you think the role will be: "))
    
    time.sleep(1)
    
    print("\n\n\nyour dice roles are: \n")
    role = int(random.randint(1,sides))
    
    time.sleep(1)
    f = open("score.txt","w")
    if role == guess:
        print("your role is" , role , "and you are correct")
        score = score + sides*2
        score = str(score)
        f.write("0"+ score)
        f.close()
        Main()
    else:
        print("your role is" , role , "and you are incorrect" )
        if score != 0:
            score = score - sides
            score = str(score)
            f.write("0"+ score)
            f.close()
            Main()
        else:
            f.write("0")
            f.close()
            Main()

path = os.getcwd() + "/score.txt"
if os.path.exists(path):
    Main()
else:
    file1 = open("score.txt","w")
    file1.write("0")
    file1.close
    Main()

So, It works well, as long as a file exists with an integer value inside, whenever i try to create the file with "0" inside it doesn't write "0" it creates the file and everything but it wont write 0. Please help.


r/pythonhelp • • Mar 03 '21

error on visual studio code

3 Upvotes

I am using visual studio code and python 3.9.2 I'm quite new to this so I probably did something dumb :)


r/pythonhelp • • Feb 27 '21

Help!! I am stuck on writing a code for this question my teacher gave me!!! I have the first half completed!

3 Upvotes

Help!!! I am stuck on making a code for this image. I have the first half down, but the "countdown" and anything after that I'm struggling with. Im not sure how to countdown from a number that a user has to provide, then subtracting that value by one and forming it in the structure shown in the image. I need help on this ASAP cause its already late.