Hướng dẫn how to return input in python - làm thế nào để trả lại đầu vào trong python

-1

Mới! Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn. Tìm hiểu thêm.
Learn more.

Tôi đang cố gắng tạo ra một vòng lặp sẽ khiến tôi yêu cầu đặt cược của người dùng cho đến khi nó hết tiền hoặc quyết định bỏ việc. Làm thế nào tôi có thể lấy đầu vào từ các chức năng khác nhau?

import random import sys def greeting(): print('COP1000 Slot Machine - Jesus Pacheco') print('Let\'s play slots!') # Function ask user starting amout to bet # then it will check for any input error def intro(): greeting() print('How much money do you want to start with?') print('Enter the starting amount of dollars.', end='') total = int(input()) print('Your current total is '+ str(total)+'\n') while True: print('How much money do you want to bet (enter 0 to quit)?', end=''); # Bett will be the amount of money that the player will bet bett = int(input()) if bett == 0: sys.exit() if bett > int(total): print('ERROR You don\'t have that much left') elif bett < int(0): print('ERROR: Invalid bet amount\n') else: break # Function will ask user to press enter to play slot machine def slotMachine(): print('Press enter to pull slot machine handle!') input() # Function shows results of slot machine after handle being pulled def random(): import random num1 = random.randint(1, 5) num2 = random.randint(1, 5) num3 = random.randint(1, 5) print('/---+---+---\ ') print('|-'+ str (num1)+'-|-'+ str(num2) +'-|-'+ str (num3) +'-|') print('\---+---+---/ ') if num1 == num2 and num2 == num3: print('JACKPOT! cha-ching, you win') if num1 == num3 or num2 == num3: print('Match two, you get your bet back!') else: print('sorry, no match') intro() slotMachine() random() input()

Đã hỏi ngày 22 tháng 2 năm 2013 lúc 23:38Feb 22, 2013 at 23:38

1

Những gì bạn cần làm là tạo một cách để có được đầu vào, trả lại, kiểm tra xem nó có hợp lệ không, sau đó gọi chức năng máy đánh bạc. Ngoài ra, bạn có thể sử dụng import sys import random def main(): intro() total = int(input('Starting Money: ')) if total <= 0: raise ValueError('Starting Money must be greater than 0.') # the above raises an error if the bet is less than 0 while True: bet = getInput(total) total = slotMachine(total, bet) if total == 'win' or total <= 0: break def intro(): print('COP1000 Slot Machine - Jesus Pacheco') print("Let's play slots!") def getInput(total): userInput = int(input('Bet (0 to quit): ')) if userInput > total or userInput < 0: raise ValueError('Bet must be less than total and greater than 0.') if userInput== 0: sys.exit() # the above raises an error if the bet is less than 0 or greater than the total return userInput def slotMachine(total, bet): num1 = random.randint(1, 5) num2 = random.randint(1, 5) num3 = random.randint(1, 5) print('/---+---+---\ ') print('|-'+ str(num1)+'-|-'+ str(num2) +'-|-'+ str(num3) +'-|') print('\---+---+---/ ') if num1 == num2 and num2 == num3: print('JACKPOT! Cha-Ching, you win!') return 'win' elif num1 == num3 or num2 == num3: print('Match two, you get your bet back!') return total else: print('Sorry, no match.') return total - bet main() 0; import sys import random def main(): intro() total = int(input('Starting Money: ')) if total <= 0: raise ValueError('Starting Money must be greater than 0.') # the above raises an error if the bet is less than 0 while True: bet = getInput(total) total = slotMachine(total, bet) if total == 'win' or total <= 0: break def intro(): print('COP1000 Slot Machine - Jesus Pacheco') print("Let's play slots!") def getInput(total): userInput = int(input('Bet (0 to quit): ')) if userInput > total or userInput < 0: raise ValueError('Bet must be less than total and greater than 0.') if userInput== 0: sys.exit() # the above raises an error if the bet is less than 0 or greater than the total return userInput def slotMachine(total, bet): num1 = random.randint(1, 5) num2 = random.randint(1, 5) num3 = random.randint(1, 5) print('/---+---+---\ ') print('|-'+ str(num1)+'-|-'+ str(num2) +'-|-'+ str(num3) +'-|') print('\---+---+---/ ') if num1 == num2 and num2 == num3: print('JACKPOT! Cha-Ching, you win!') return 'win' elif num1 == num3 or num2 == num3: print('Match two, you get your bet back!') return total else: print('Sorry, no match.') return total - bet main() 1 là lời nhắc bạn muốn hiển thị (import sys import random def main(): intro() total = int(input('Starting Money: ')) if total <= 0: raise ValueError('Starting Money must be greater than 0.') # the above raises an error if the bet is less than 0 while True: bet = getInput(total) total = slotMachine(total, bet) if total == 'win' or total <= 0: break def intro(): print('COP1000 Slot Machine - Jesus Pacheco') print("Let's play slots!") def getInput(total): userInput = int(input('Bet (0 to quit): ')) if userInput > total or userInput < 0: raise ValueError('Bet must be less than total and greater than 0.') if userInput== 0: sys.exit() # the above raises an error if the bet is less than 0 or greater than the total return userInput def slotMachine(total, bet): num1 = random.randint(1, 5) num2 = random.randint(1, 5) num3 = random.randint(1, 5) print('/---+---+---\ ') print('|-'+ str(num1)+'-|-'+ str(num2) +'-|-'+ str(num3) +'-|') print('\---+---+---/ ') if num1 == num2 and num2 == num3: print('JACKPOT! Cha-Ching, you win!') return 'win' elif num1 == num3 or num2 == num3: print('Match two, you get your bet back!') return total else: print('Sorry, no match.') return total - bet main() 1 thường là một chuỗi). Tôi khuyên bạn nên đặt tất cả những thứ này theo hàm import sys import random def main(): intro() total = int(input('Starting Money: ')) if total <= 0: raise ValueError('Starting Money must be greater than 0.') # the above raises an error if the bet is less than 0 while True: bet = getInput(total) total = slotMachine(total, bet) if total == 'win' or total <= 0: break def intro(): print('COP1000 Slot Machine - Jesus Pacheco') print("Let's play slots!") def getInput(total): userInput = int(input('Bet (0 to quit): ')) if userInput > total or userInput < 0: raise ValueError('Bet must be less than total and greater than 0.') if userInput== 0: sys.exit() # the above raises an error if the bet is less than 0 or greater than the total return userInput def slotMachine(total, bet): num1 = random.randint(1, 5) num2 = random.randint(1, 5) num3 = random.randint(1, 5) print('/---+---+---\ ') print('|-'+ str(num1)+'-|-'+ str(num2) +'-|-'+ str(num3) +'-|') print('\---+---+---/ ') if num1 == num2 and num2 == num3: print('JACKPOT! Cha-Ching, you win!') return 'win' elif num1 == num3 or num2 == num3: print('Match two, you get your bet back!') return total else: print('Sorry, no match.') return total - bet main() 3:

import sys import random def main(): intro() total = int(input('Starting Money: ')) if total <= 0: raise ValueError('Starting Money must be greater than 0.') # the above raises an error if the bet is less than 0 while True: bet = getInput(total) total = slotMachine(total, bet) if total == 'win' or total <= 0: break def intro(): print('COP1000 Slot Machine - Jesus Pacheco') print("Let's play slots!") def getInput(total): userInput = int(input('Bet (0 to quit): ')) if userInput > total or userInput < 0: raise ValueError('Bet must be less than total and greater than 0.') if userInput== 0: sys.exit() # the above raises an error if the bet is less than 0 or greater than the total return userInput def slotMachine(total, bet): num1 = random.randint(1, 5) num2 = random.randint(1, 5) num3 = random.randint(1, 5) print('/---+---+---\ ') print('|-'+ str(num1)+'-|-'+ str(num2) +'-|-'+ str(num3) +'-|') print('\---+---+---/ ') if num1 == num2 and num2 == num3: print('JACKPOT! Cha-Ching, you win!') return 'win' elif num1 == num3 or num2 == num3: print('Match two, you get your bet back!') return total else: print('Sorry, no match.') return total - bet main()

Tôi đề nghị bạn đọc về các chức năng và câu lệnh trả lại.

Đã trả lời ngày 22 tháng 2 năm 2013 lúc 23:48Feb 22, 2013 at 23:48

Rushy Panchalrushy PanchalRushy Panchal

16.2K16 Huy hiệu vàng58 Huy hiệu bạc92 Huy hiệu Đồng16 gold badges58 silver badges92 bronze badges

1

  • Loại giá trị nào mà Input () trả về?
  • Giá trị được trả về bởi đầu vào () là một chuỗi. Bất kỳ loại dữ liệu có thể được sử dụng để chuyển đổi nội dung của đầu vào. Ví dụ: người dùng có thể chuyển đổi giá trị được nhập vào một biến số nguyên.
  • Đầu vào của người dùng Python từ bàn phím có thể được đọc bằng hàm tích hợp đầu vào ().
  • Đầu vào từ người dùng được đọc dưới dạng chuỗi và có thể được gán cho một biến.
  • Sau khi nhập giá trị từ bàn phím, chúng tôi phải nhấn nút Enter Enter. Sau đó, hàm input () đọc giá trị được nhập bởi người dùng.(*nix: Ctrl-D, Windows: Ctrl-Z+Return), EOFError is raised and the program is terminated.

Chương trình dừng lại vô thời hạn cho đầu vào của người dùng. Không có tùy chọn để cung cấp giá trị thời gian chờ.

Nếu chúng ta nhập EOF (*NIX: Ctrl-D, Windows: Ctrl-Z+Return), Eoferror sẽ được nâng lên và chương trình bị chấm dứt.

Chức năng cú pháp của đầu vào ()

Hàm cú pháp của hàm input () là:

Chuỗi nhắc được in trên bảng điều khiển và điều khiển được cung cấp cho người dùng để nhập giá trị. Bạn nên in một số thông tin hữu ích để hướng dẫn người dùng nhập giá trị dự kiến.

value = input("Please enter a string:\n") print(f'You entered {value}')

Output:

Nhận đầu vào của người dùng trong Python

Dưới đây là một ví dụ đơn giản về việc lấy đầu vào của người dùng và in nó trên bảng điều khiển.

Đầu vào của người dùng Python

value = input("Please enter a string:\n") print(f'You entered {value} and its type is {type(value)}') value = input("Please enter an integer:\n") print(f'You entered {value} and its type is {type(value)}')

Output:

Please enter a string: Python You entered Python and its type is <class 'str'> Please enter an integer: 123 You entered 123 and its type is <class 'str'>

Loại giá trị đã nhập của người dùng là gì?

Giá trị nhập người dùng luôn được chuyển đổi thành một chuỗi và sau đó được gán cho biến. Hãy để xác nhận điều này bằng cách sử dụng hàm loại () để lấy loại biến đầu vào.

value = input("Please enter an integer:\n") value = int(value) print(f'You entered {value} and its square is {value ** 2}')

Output:

Số nguyên đầu vào của người dùng Python

Đầu vào của người dùng Python và ví dụ về Eoferror

Khi chúng ta nhập EOF, Input () tăng Eoferror và chấm dứt chương trình. Hãy cùng xem một ví dụ đơn giản bằng cách sử dụng pycharm IDE.

value = input("Please enter an integer:\n") print(f'You entered {value}')

Output::

Please enter an integer: ^D Traceback (most recent call last): File "/Users/pankaj/Documents/PycharmProjects/PythonTutorialPro/hello-world/user_input.py", line 1, in <module> value = input("Please enter an integer:\n") EOFError: EOF when reading a line

Đầu vào của người dùng Python tăng eoferror

Ví dụ về lựa chọn đầu vào của người dùng Python

Chúng tôi có thể xây dựng một hệ thống thông minh bằng cách đưa ra sự lựa chọn cho người dùng và đưa đầu vào người dùng để tiến hành lựa chọn.

value1 = input("Please enter first integer:\n") value2 = input("Please enter second integer:\n") v1 = int(value1) v2 = int(value2) choice = input("Enter 1 for addition.\nEnter 2 for subtraction.\nEnter 3 for Multiplication.:\n") choice = int(choice) if choice == 1: print(f'You entered {v1} and {v2} and their addition is {v1 + v2}') elif choice == 2: print(f'You entered {v1} and {v2} and their subtraction is {v1 - v2}') elif choice == 3: print(f'You entered {v1} and {v2} and their multiplication is {v1 * v2}') else: print("Wrong Choice, terminating the program.")

Dưới đây là một đầu ra mẫu từ việc thực hiện chương trình trên.

Lựa chọn đầu vào của người dùng Python

Từ nhanh trên hàm python raw_input ()

Hàm raw_input () được sử dụng để lấy đầu vào của người dùng trong các phiên bản Python 2.x. Dưới đây là một ví dụ đơn giản từ trình thông dịch dòng lệnh Python 2.7 hiển thị việc sử dụng hàm raw_input ().

~ python2.7 Python 2.7.10 (default, Feb 22 2019, 21:55:15) [GCC 4.2.1 Compatible Apple LLVM 10.0.1 (clang-1001.0.37.14)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> value = raw_input("Please enter a string\n") Please enter a string Hello >>> print value Hello

Chức năng này đã được không dùng nữa và loại bỏ khỏi Python 3. Nếu bạn vẫn còn trên các phiên bản Python 2.x, thì bạn nên nâng cấp lên các phiên bản Python 3.x.

Sự kết luận

Nó rất dễ dàng để lấy đầu vào người dùng trong python từ hàm input (). Nó chủ yếu được sử dụng để cung cấp sự lựa chọn hoạt động cho người dùng và sau đó thay đổi luồng của chương trình cho phù hợp.

Tuy nhiên, chương trình chờ đợi vô thời hạn cho đầu vào của người dùng. Thật tuyệt khi có một số thời gian chờ và giá trị mặc định trong trường hợp người dùng không nhập giá trị kịp thời.

References:

  • Tài liệu Python.org Input ()

Làm cách nào để nhận đầu vào người dùng trong Python?

Đầu vào của người dùng Python..
❮ Trước Sau ❯.
Python 3.6.Tên người dùng = Đầu vào ("Nhập tên người dùng:") In ("Tên người dùng là:" + Tên người dùng) Chạy ví dụ ».
Python 2.7.Tên người dùng = RAW_Input ("Nhập tên người dùng:") In ("Tên người dùng là:" + Tên người dùng) Chạy ví dụ ».
❮ Trước Sau ❯.

Python 3.6.Tên người dùng = Đầu vào ("Nhập tên người dùng:") In ("Tên người dùng là:" + Tên người dùng) Chạy ví dụ ».

Bạn có thể nhớ lại các mục trong danh sách bằng cách nhập tên của danh sách và sau đó số lượng đã đặt hàng của mục bạn đang tìmNhớ lại 'Những chú hề', MyFears [3] nhớ lại 'Người gửi thư của tôi'.typing the name of the list and then the ordered number of the item you're looking for, however, be aware that numbering in Python begins with 0 rather than 1. Ex: myFears[0] will recall 'clowns', myFears[3] recalls 'my mailman'.

Loại trả về chức năng đầu vào Python là gì?

Input () - Đọc đầu vào và trả về một loại python như danh sách, tuple, int, v.v.list, tuple, int, etc.

Loại giá trị nào mà Input () trả về?

Giá trị được trả về bởi đầu vào () là một chuỗi.Bất kỳ loại dữ liệu có thể được sử dụng để chuyển đổi nội dung của đầu vào.Ví dụ: người dùng có thể chuyển đổi giá trị được nhập vào một biến số nguyên.a string. Any data type can be used to convert the contents of an input. For example: The user can convert the value entered into an integer variable.

Chủ đề