Python input() funktion bruges til at tage brugerinput. Som standard returnerer det brugerinput i form af en streng.
input() Funktion
Syntaks:
input(prompt)>prompt [valgfrit]: enhver strengværdi, der skal vises som inputmeddelelse
Eks: input (Hvad er dit navn?)
Vender tilbage: Returner en strengværdi som input af brugeren.
By default input() function helps in taking user input as string. If any user wants to take input as int or float, we just need to typecast it.>
Python3
json filen
# Taking input as string> color>=> input>(>'What color is rose?: '>)> print>(color)> # Taking input as int> # Typecasting to int> n>=> int>(>input>(>'How many roses?: '>))> print>(n)> # Taking input as float> # Typecasting to float> price>=> float>(>input>(>'Price of each rose?: '>))> print>(price)> |
java andet hvis
>
>
Produktion:
What color is rose?: red red How many roses?: 10 10 Price of each rose?: 15.50 15.5>
Eksempel 1: At tage det Brugerens navn og alder som input og udskrivning
Som standard returnerer input en streng. Så navnet og alderen vil blive gemt som strenge.
Python
# Taking name of the user as input> # and storing it name variable> name>=> input>(>'Please Enter Your Name: '>)> # taking age of the user as input and> # storing in into variable age> age>=> input>(>'Please Enter Your Age: '>)> print>(>'Name & Age: '>, name, age)> |
>
fmovies indien
>
Produktion:
Please Enter Your Name: Rohit Please Enter Your Age: 16 Name & Age: Rohit 16>
Eksempel 2: At tage to heltal fra brugere og tilføje dem.
I dette eksempel vil vi se på, hvordan man tager heltalsinput fra brugere. For at tage heltalsinput vil vi bruge int() sammen med Python input()
Python
efterfølgende datatyper
# Taking number 1 from user as int> num1>=> int>(>input>(>'Please Enter First Number: '>))> # Taking number 2 from user as int> num2>=> int>(>input>(>'Please Enter Second Number: '>))> # adding num1 and num2 and storing them in> # variable addition> addition>=> num1>+> num2> # printing> print>(>'The sum of the two given numbers is {} '>.>format>(addition))> |
>
>
Produktion:

På samme måde kan vi bruge float() til at tage to flydende tal. Lad os se endnu et eksempel på, hvordan man tager lister som input
Eksempel 3: At tage to lister som input og tilføje dem
Tager brugerinput som en streng og opdeler hvert tegn ved hjælp af list() for at konvertere til en liste over tegn.
Python
hvordan man sletter kolonne i postgresql
# Taking list1 input from user as list> list1>=> list>(>input>(>'Please Enter Elements of list1: '>))> # Taking list2 input from user as list> list2>=> list>(>input>(>'Please Enter Elements of list2: '>))> # appending list2 into list1 using .append function> for> i>in> list2:> >list1.append(i)> # printing list1> print>(list1)> |
>
>
Produktion:
