简体   繁体   English

在python 3中为单元格分配用户输入值

[英]assigning to a cell a user input value in python 3

i've been tasked with 我被分配了

"Ask the user to select a cell in the table and a number to put into the cell" from a partial magic square table. 从局部幻方表中“要求用户选择表中的一个单元格和要放入该单元格中的数字”。

so far I have 到目前为止,我有

numbers = input("Enter two integers: ")
numbers = numbers.split(" ")
y = [int(x) for x in numbers]
print(y)

this gives me the numbers, but how do I make them relate to columns and rows? 这给了我数字,但是如何使它们与列和行相关? basically, if user inputs '1 and 2', how can I relate them to a column number 1 and row number 2? 基本上,如果用户输入“ 1和2”,如何将它们与列号1和行号2相关联?

thankyou 谢谢

Ask the user to select a cell in the table 要求用户在表格中选择一个单元格

This code prompts for a string of two numbers and converts the first two numbers separated by space into integers. 此代码提示输入两个数字组成的字符串,并将前两个数字(由空格分隔)转换为整数。 This is more correct than y = [int(x) for x in numbers] because y would be a list of ints, not an int itself. 这比y = [int(x) for x in numbers]更正确,因为y是一个int列表,而不是int本身。

numbers = input("Enter two integers: ")
x, y = map(int, numbers.split(" "))
print("row", y, "col", x)

and a number to put into the cell 和一个要放入单元格的数字

Assuming you have a 2D list square variable that you can access by row and column... Just assign the cell value 假设您有一个二维列表square变量,可以按行和列进行访问...只需分配单元格值

cell_value = input("Enter a cell value")
square[y][x]  = int(cell_value) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM