Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8982143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:30:44+00:00 2026-06-15T20:30:44+00:00

I wrote a program to solve y = a^x and then project it on

  • 0

I wrote a program to solve y = a^x and then project it on a graph. The problem is that whenever a < 1 I get the error:

ValueError: invalid literal for int () with base 10.

Any suggestions?

Here’s the traceback:

Traceback (most recent call last): 
   File "C:\Users\kasutaja\Desktop\EksponentfunktsioonTEST - koopia.py", line 13, in <module> 
   if int(a) < 0: 
ValueError: invalid literal for int() with base 10: '0.3' 

The problem arises every time I put a number that is smaller than one, but larger than 0. For this example it was 0.3 .

This is my code:

#  y = a^x

import time
import math
import sys
import os
import subprocess
import matplotlib.pyplot as plt
print ("y = a^x")
print ("")
a = input ("Enter 'a' ")
print ("")
if int(a) < 0:
    print ("'a' is negative, no solution")
elif int(a) == 1:
    print ("'a' is equal with 1, no solution")
else:
    fig = plt.figure ()
    x = [-2,-1.75,-1.5,-1.25,-1,-0.75,-0.5,-0.25,0,0.25,0.5,0.75,1,1.25,1.5,1.75,2]
    y = [int(a)**(-2),int(a)**(-1.75),int(a)**(-1.5),int(a)**(-1.25),
            int(a)**(-1),int(a)**(-0.75),int(a)**(-0.5),int(a)**(-0.25),
            int(a)**(0),int(a)**(0.25),int(a)**(0.5),int(a)**(0.75),
            int(a)**1,int(a)**(1.25),int(a)**(1.5),int(a)**(1.75), int(a)**(2)]


    ax = fig.add_subplot(1,1,1)
    ax.set_title('y = a**x')
    ax.plot(x,y)
    ax.spines['left'].set_position('zero')
    ax.spines['right'].set_color('none')
    ax.spines['bottom'].set_position('zero')
    ax.spines['top'].set_color('none')
    ax.spines['left'].set_smart_bounds(True)
    ax.spines['bottom'].set_smart_bounds(True)
    ax.xaxis.set_ticks_position('bottom')
    ax.yaxis.set_ticks_position('left')


    plt.savefig("graph.png")
    subprocess.Popen('explorer "C:\\Users\\kasutaja\\desktop\\graph.png"')

def restart_program(): 
    python = sys.executable
    os.execl(python, python, * sys.argv)

if __name__ == "__main__":
    answer = input("Restart program? ")
    if answer.strip() in "YES yes Yes y Y".split():
        restart_program()
    else:
        os.remove("C:\\Users\\kasutaja\\desktop\\graph.png")
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T20:30:45+00:00Added an answer on June 15, 2026 at 8:30 pm

    Answer:

    Your traceback is telling you that int() takes integers, you are trying to give a decimal, so you need to use float():

    a = float(a)
    

    This should work as expected:

    >>> int(input("Type a number: "))
    Type a number: 0.3
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ValueError: invalid literal for int() with base 10: '0.3'
    >>> float(input("Type a number: "))
    Type a number: 0.3
    0.3
    

    Computers store numbers in a variety of different ways. Python has two main ones. Integers, which store whole numbers (ℤ), and floating point numbers, which store real numbers (ℝ). You need to use the right one based on what you require.

    (As a note, Python is pretty good at abstracting this away from you, most other language also have double precision floating point numbers, for instance, but you don’t need to worry about that. Since 3.0, Python will also automatically convert integers to floats if you divide them, so it’s actually very easy to work with.)

    Previous guess at answer before we had the traceback:

    Your problem is that whatever you are typing is can’t be converted into a number. This could be caused by a lot of things, for example:

    >>> int(input("Type a number: "))
    Type a number: -1
    -1
    >>> int(input("Type a number: "))
    Type a number: - 1
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ValueError: invalid literal for int() with base 10: '- 1'
    

    Adding a space between the - and 1 will cause the string not to be parsed correctly into a number. This is, of course, just an example, and you will have to tell us what input you are giving for us to be able to say for sure what the issue is.

    Advice on code style:

    y = [int(a)**(-2),int(a)**(-1.75),int(a)**(-1.5),int(a)**(-1.25),
                int(a)**(-1),int(a)**(-0.75),int(a)**(-0.5),int(a)**(-0.25),
                int(a)**(0),int(a)**(0.25),int(a)**(0.5),int(a)**(0.75),
                int(a)**1,int(a)**(1.25),int(a)**(1.5),int(a)**(1.75), int(a)**(2)]
    

    This is an example of a really bad coding habit. Where you are copying something again and again something is wrong. Firstly, you use int(a) a ton of times, wherever you do this, you should instead assign the value to a variable, and use that instead, avoiding typing (and forcing the computer to calculate) the value again and again:

    a = int(a)
    

    In this example I assign the value back to a, overwriting the old value with the new one we want to use.

    y = [a**i for i in x]
    

    This code produces the same result as the monster above, without the masses of writing out the same thing again and again. It’s a simple list comprehension. This also means that if you edit x, you don’t need to do anything to y, it will naturally update to suit.

    Also note that PEP-8, the Python style guide, suggests strongly that you don’t leave spaces between an identifier and the brackets when making a function call.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a program to solve a complicated problem. This program is just a
I wrote a simple program to solve the math problem: A^2+B^2 = 12 A*B
I wrote a little program that solves 49151 sudoku's within an hour for an
I wrote a program called Hello.py that looks like this: import pygame, sys from
I wrote this program: #include <stdio.h> /*Part B Write a program that: defines an
I wrote a program that forks some processes with fork(). I want to kill
I'm working on a program for class that involves solving the Chinese Postman problem
I am writing a program to try to solve a math problem. I need
I wrote a program to generate a maze and to solve it, however it
I decided to try project euler problem 17 today, and I quickly wrote a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.