How to use sys.exit() if input is equal to specific number

2024/11/19 4:24:47

I am looking to correct this code so that when the user inputs 99999 then the code stops running, im also looking to make it so that if the user input is 999 it sets the total to 0

import sysdef money_earned():total = int()try: # try open the filefile = open("total.txt", "r") data = file.readline()total = int(data)except: # if file does not existfile = open("total.txt", "w+") # create filetotal = 0file.close() # close file for nowwhile True:try:pay_this_week = int(input("How much money did you earn this week? "))breakexcept ValueError:print("Oops! That was no valid number. Try again...")pay_this_week_message = "You've earned £{0} this week!".format(pay_this_week)total = pay_this_week + totaltotal_message = "You have earned £{0} in total!".format(total)print(pay_this_week_message)print(total_message)if pay_this_week == "99999":sys.exit()file = open("total.txt", "w") # wipe the file and let us write to itfile.write(str(total)) # write the datafile.close() # close the filemoney_earned()
Answer

So you're taking the input as a string and immediately converting it to an int, but you could actually convert it to an int later and check for some words in your input first.

Right now you have

 pay_this_week = int(input("..."))

but if you change this to

input_from_user = input("...")
pay_this_week = int(input_from_user)

then we can add some more code inbetween

input_from_user = input("...")
if input_from_user == "done":return  # this will exit the function and so end execution
elif input_from_user == "reset":total = 0 # reset the total
else:pay_this_week = int(input_from_user)

this should have the desired effect

https://en.xdnf.cn/q/119992.html

Related Q&A

python about linking sublist with same number together

I need to group sublists with the same elements together For example:list1 =[[1, 0], [2, 1], [30, 32]]would link [1, 0] and [2, 1] together since they both contain 1 and those two would combine into [0…

what to fix to solve the TypeError: an integer is required (got type str) [duplicate]

This question already has answers here:How can I read inputs as numbers?(10 answers)Closed 6 years ago.I have a problem with the choice of calculation years. python flux2nc.py ../data/output/fluxes/ …

Multiprocessing in python/beautifulsoup issues

Hi guys im fairly new in python. what im trying to do is to move my old code into multiprocessing however im facing some errors that i hope anyone could help me out. My code is used to check a few thou…

Fastest possible generation of permutation with defined element values in Python

Trying to generate permutations, could be used with generator or produced List of Lists (but maybe I need a lot of memory?) Looked on the Internet and SO, but couldnt find a version where I define the…

Obtain coordinates of a Polygon / Multi-polygon around a point in python [duplicate]

This question already has answers here:Draw a polygon around point in scattermapbox using python(2 answers)Closed 2 years ago.I am using plotlys scattermapbox to draw a polygon around a point object. I…

PyCuda Error in Execution

This is my pycuda code for rotation.I have installed the latest cuda drivers and I use a nvidia gpu with cuda support.I have also installed the cuda toolkit and pycuda drivers.Still I get this strange …

Python code to Download Specific JSON key value data through REST API calls

I am trying to write a code in python which download only specific key value in the Calls. So the solution might beDownloading the Raw data and later removing the unwanted through regex or 2)Applying s…

How to measure pairwise distances between two sets of points?

I have two datasets (csv files). Both of them contains latitudes-longitudes of two sets (220 and 4400) of points. Now I want to measure pairwise distances (miles) between these two sets of points (220 …

Interactively Re-color Bars in Matplotlib Bar Chart using Confidence Intervals

Trying to shade the bars in this chart based on the confidence that a selected y-value (represented by the red line) lies within a confidence interval. See recolorBars() method in the class example bel…

Unlock password protected Workbook using VBA or Python

I have a workbook name m.xlsx, but its password protected and Ive forgotten the password. How can I open it or un-protect it?The following code does not work:Unprotect workbook without password I need…