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

  • Home
  • SEARCH
  • 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 7628663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:38:07+00:00 2026-05-31T05:38:07+00:00

So I have the code: def logdata(x, y): try: f = open(‘multlog.txt’, ‘a’) f.write({0:g}

  • 0

So I have the code:

def logdata(x, y):
    try:
        f = open('multlog.txt', 'a')
        f.write("{0:g} * {1:g} = {2:g}\n".format(x,y, (x*y)))
    except ValueError:
        f.write("Error, you tried to multiply by something that wasn't a number")
        raise
    finally:
        f.close()
print("This is a test program, it logs data in a text file, 'multlog.txt'")
fn = input("Enter the first number you'd like to multiply by: ")
sn = input("Enter the second number you'd like to multiply by: ")
logdata(int(fn), int(sn))

And what I want it to do, is when it reaches a value error, for it to write to the file,”Error, you tried to multiply by something that wasn’t a number”. But, if the file reaches a value error if the user inputs a letter, say “j”,ValueError: invalid literal for int() with base 10: 'j', it doesn’t write to the file!

  • 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-05-31T05:38:08+00:00Added an answer on May 31, 2026 at 5:38 am

    At least two problems:

    1. The file is not open for writing (or appending) in the except block.
    2. As @DSM points out in a comment, the ValueError is being raised when you call int()

    I would rewrite to something like the below example.
    If you use the with statement then you can do without the finally block.

    def logdata(x, y):
        with open('multlog.txt', 'a') as f:
            try:
                x = int(x); y = int(y)
                f.write("{0:g} * {1:g} = {2:g}\n".format(x,y, (x*y)))
            except ValueError:
                f.write("Error")
    
    print("This is a test program, it logs data in a text file, 'multlog.txt'")
    fn = input("Enter the first number you'd like to multiply by: ")
    sn = input("Enter the second number you'd like to multiply by: ")
    logdata(fn, sn)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: def causes_exception(lamb): try: lamb() return False except: return True
Lets say I have the code: class NoneDict(dict): def __getitem__(self, name): try: return super(NoneDict,
So, I have the following code: def LSCPHandler.send_message(message, hostname, port) s = TCPSocket.open(hostname, port)
I have this code: def setVelocity (x, y, yaw) setVelocity (Command2d.new(x,y,yaw)) end def setVelocity
I have this code: def display(self): print self.doc.toprettyxml(indent= ) strigName ='/Users/my_user/Desktop/python/' + str(datetime.datetime.now()) +
I have code: class Scene def initialize(number) @number = number end attr_reader :number end
i have the following code def get(self): date = datetime.date.today() loc_query = Location.all() last_cursor
I have the following code: def input_scores(): scores = [] y = 1 for
I have code like this (simplified): def outer(): ctr = 0 def inner(): ctr
Let's say we have code looks like this module TM def aa puts aa

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.