Receive input that is separated by spaces and contains a variety of data types

I need to assign 4 values from user input to 4 separate variables, each separated by a space.

For instance:

x y 3 7

The first two values are strings, while the other two values are integers. How can I achieve this?

Answer №1

Take a look at this code snippet for using raw_input in Python 2 (and input in Python 3):

>>> inp = raw_input("Enter space separated values of form 'a b 2 5' \n")
Enter space separated values of form 'a b 2 5' 
a b 2 5
>>> vars = [int(i) if i.isdigit() else i for i in inp.split()]
>>> vars
['a', 'b', 2, 5]

Essentially, you can input multiple space-separated values and then split them later on.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Guide to automatically running a Chrome extension that interacts with the main webpage?

I am looking for a way to automate testing for a specific chrome extension. While I have successfully used selenium-python to automate tasks on the parent web-page, I am facing a challenge automating the chrome-extension itself. Selenium is not designed t ...

Ways to identify the presence of an optional term

Currently, I am utilizing regex for file processing. For instance, if I have the lines below, my goal is to extract the Example number and determine whether there is an ERROR present: Example 1: bla bla bla Example 2: bla bla ERROR Example 3: bla bla My ...

Troubleshooting a shape issue in a basic neural network using TensorFlow

I have a set of data points represented as (x1, y1), (x2, y2) ... Unfortunately, I do not know the relationship equation between x and y. That's why I decided to utilize neural network techniques in order to identify it. There is a file called hyper ...

Obtaining information from connected brushes in mlpd3, Bokeh, and Plotly using Python

In the following code, a 2x2 graph with 4 plots is generated. Data points can be selected using brushes. The main question here is how to retrieve the selected data points as a JSON array or CSV file. While this code uses mpld3 for this purpose, Bokeh offe ...

Encountering an issue with Selenium and FirefoxProfile while trying to load a profile. A Python script is being called from PHP

While running my Python script, I encountered the following code: firefox_profile = webdriver.FirefoxProfile() self.driver = webdriver.Firefox(firefox_profile=firefox_profile) Executing the script from bash was successful. However, when attempting to cal ...

What is the best way to extract data from the JSON output of a gerrit query?

I'm facing a situation where I need to retrieve specific data from the output of a gerrit query, but unfortunately, I'm having difficulty using awk. Here is the command I am running: ssh -p 29418 gerrit.abc.se gerrit query --format=JSON project ...

What is the best way to terminate Selenium Chrome drivers generated from multiprocessing.Pool?

I implemented a system where I have a roster of article titles and IDs that are utilized to construct the URLs for the articles and then collect their content through web scraping. To optimize the process, I decided to employ multiprocessing.Pool for paral ...

What is the reason for the Python 3 command executing Python 3.9 instead of Python 3.10?

On my Mac, I have Python installed and when executing certain scripts I've created, I encounter a "module not found" error if they contain packages. The issue arises because I run my scripts using "python3 <script_name.py>". To utilize all the p ...

What is the best approach for handling a column in Pandas that is labeled as "NA"?

I am encountering a problem with the data in my spreadsheet that shows as "NA" when I try to read it using Pandas. The issue is that "NA" (which stands for "North America" in my case) gets converted to "NaN" during the process. Does this mean that "NA" is ...

Tips for organizing a dataframe with numerous NaN values and combining all rows that do not begin with NaN

Below is a df that I have: df = pd.DataFrame({ 'col1': [1, np.nan, np.nan, np.nan, 1, np.nan, np.nan, np.nan], 'col2': [np.nan, 2, np.nan, np.nan, np.nan, 2, np.nan, np.nan], 'col3': [np.nan, np.nan, 3, np.nan, np. ...

Rate according to the truth table without employing if-else statements

I am working with a list of dictionaries, each containing boolean flags. The structure of the list is as follows: listA = [{key1: somevalue1, key2:somevalue2, flag1:True, flag2:False, score:0}, {key1: somevalue3, key2:somevalue4, flag1:False, fla ...

Is there a way to effortlessly include the root directory to the python path in VSCode?

I recently made the switch from PyCharm to VSCode and I am encountering an issue with importing modules within the same package. main.py from importlib_resources import files import household_prices.data.raw_data as raw_data # <- module not found sou ...

Tips for passing a parameterized method as an argument to another function in Python

I understand that the following code is valid: def printValue(): print 'This is the printValue() method' def callPrintValue(methodName): methodName() print 'This is the callPrintValue() method' However, I am wondering if ...

selenium.common.exceptions.InvalidArgumentException: Error: invalid argument encountered when looping through a list of URLs and passing it as an argument to the get() function

Currently, I am extracting URLs from a web page to later use them for scraping a plethora of information. My goal is to streamline the process and avoid manual copying and pasting. However, I seem to be facing an issue in making the get() function work wit ...

Utilizing pandas to extract data within specified time ranges from a DataFrame

Looking at a segment of my dataFrame: fruit time 0 apple 2021-12-20 17:55:00 1 bannana 2021-12-23 05:13:00 2 apple 2021-12-20 17:55:00 I'm curious about how to extract data between specific timestamps, like from 5: ...

Generating a NumPy array from a list using operations

I retrieved data from an SQLite database stored in a Python list with the following structure: # Here is an example data = [(1, '12345', 1, 0, None), (1, '34567', 1, 1, None)] My goal is to convert this list of tuples into a 2D NumPy a ...

tips for integrating html5 elements with django forms

I am interested in utilizing the following code: # extra.py in yourproject/app/ from django.db.models import FileField from django.forms import forms from django.template.defaultfilters import filesizeformat from django.utils.translation import ugettext_ ...

What causes the unrecognized command line error to occur while using distutils to build a C extension?

When attempting to construct a source using the python's distutils, I encountered an issue. I crafted a basic setup.py following guidance from this example, and executing the build as recommended resulted in success: python setup.py build Now, it is ...

Error: Cannot compare instances of 'datetime.date' and 'str' using the '>' operator

Within the code snippet provided, my objective is to validate whether a file that has been dropped into a directory is indeed a new file that needs to be processed. To achieve this, I aim to compare the creation date of the new file with the creation date ...

The attempt to import the SWIG library fails as the dynamic module does not specify the initialization function

I have encountered a problem with my code: double My_variable = 3.0; I also have a file named example_python.i: %module example %{ extern double My_variable; %} After executing the following commands: swig -python example_python.i gcc -o example.o -c ...