Why does input() always return a string?

2024/11/16 15:43:18

Here is my code:

age = input("How old are you?: ")
print (age)
print (type(age))

Result:

How old are you?: 35
35
class 'str' <<--- This is problem!

But, If I use..

age = int(input("How old are you?: "))
print (age) 
print (type(age))

and entered "Alex"

How old are you?: alex
Traceback (most recent call last):
File "/Users/kanapatgreenigorn/Desktop/test.py", line 1, in age = int(input("How old are you?: "))ValueError: invalid literal for int() with base 10: 'alex'

Answer

If you want it to return string then I think it does that by default but if you want it to return like a number or integer then you can put the int( in front of the input. You put in 'alex' and the reason why it came as an error was because 'alex' is a string. not an integer (which is int)

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

Related Q&A

Windowed mode cannot run

Why does pyinstaller exe not run in windowed mode but fine without it? I have changed over to a windows OS from Linux. Never had any issue before hand, how do I correct this.

inserting a variable into an fstring using .replace()

I have a code something similar to bellow. name = Dave message = f<name> is a really great guy! message = message.replace(<name>, {name}) print(message)the variables are a little more compl…

How to allow caps in this input box program for pygame?

I found this input box module on the internet but it only allows lower case no upper. So could someone tell me what to change in the module to allow caps as im creating a small multiplayer game and i n…

Why testing error rate increases at high values of K in KNN algorithm?

I am getting the error rates like this up to 20 values what might be the reason for this ?k_values: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] Error [0.0, 0.0, 0.0, 0.0, 0…

Divide and Conquer. Find the majority of element in array

I am working on a python algorithm to find the most frequent element in the list. def GetFrequency(a, element): return sum([1 for x in a if x == element])def GetMajorityElement(a):n = len(a)if n == …

Scraping dynamic webpage using Python

I am trying to scrape following dynamically generated webpage https://www.governmentjobs.com/careers/capecoral?page=1 Ive used requests, scrapy, scrapy-splash but I simply get page source code and I d…

numba cuda deprecation error : how to update my code?

Im running a jupyter notebook frome here : https://github.com/noahgift/nuclear_powered_command_line_tools/blob/master/notebooks/numba-cuda.ipynb The docs of current numba/cuda is here : https://numba.r…

reverse nested dicts using python

I already referred these posts here, here and here. I have a sample dict like as shown below t = {thisdict:{"brand": "Ford","model": "Mustang","year": …

python how to generate permutations of putting a singular character into a word

No idea how to word this so the title sucks my bad, Basically, I have a 4 letter word and I want to generate every permutation of putting a dash in it. So if my word was Cats, I want to get every permu…

Selenium Scraping Javascript Table

I am stuggling to scrape as per code below. Would apprciate it if someone can have a look at what I am missing? Regards PyProg70from selenium import webdriver from selenium.webdriver import FirefoxOp…