Biến gợi ý kiểu python

Tất cả các thứ hoạt động ngoại lệ [current_space] không được cập nhật với [current_space +=rolled_dice]. Nó chỉ cập nhật giá trị [rolled_dice]. Điều gì gây ra điều này và có hành động tốt hơn để thực hiện ở đây. Giống như một lớp học?

Nội dung chính Hiển thị

  • You can change a variable in a command if python?
  • You can set a variable in a command IF?
  • Bạn có thể tạo một biến bên trong một câu lệnh nếu python không?
  • Bạn có thể sửa đổi một biến trong Python không?

from random import randint

finish_line = 20
player_1 = raw_input['Enter player 1 name: ']
player_2 = raw_input['Enter player 2 name: ']    
print['\nWelcome ' + player_1 + ' and ' + player_2]
print['\nLet\'s Play!\n']


def roll_1[]:
    current_space1 = int[]
    #print[current_space1]
    roll_dice = raw_input[player_1 + ' roll dice? y or n: ']
    if roll_dice == 'y':
        rolled_dice = [randint[1,6]]
        print[player_1 + ' ' + 'rolled a ' + str[rolled_dice]]
        if current_space1 != finish_line:
            current_space1 += rolled_dice
            #print[current_space1]
            roll_2[]
        elif current_space1 == finish_line:
            print['You are the winner ' + player_1 + '!']
        elif roll_dice == 'n':
            print['Thanks for playing']
        else:
            print['Invalid entry']



def roll_2[]:
    current_space2 = int[]
    roll_dice = raw_input[player_2 + ' roll dice? y or n: ']
    if roll_dice == 'y':
        rolled_dice = [randint[1,6]]
        print[player_2 + ' ' + 'rolled a ' + str[rolled_dice]]
        if current_space2 != finish_line:
            current_space2 += rolled_dice
            roll_1[]
        elif current_space2 == finish_line:
            print['You are the winner ' + player_2 + '!']
        elif roll_dice == 'n':
            print['Thanks for playing']
        else:
            print['Invalid entry']

roll_1[] 

Khi được hỏi ngày 5 tháng 5 năm 2017 lúc 22. 20 5 tháng 5 năm 2017 lúc 22. 20

Cả

finish_line = 20
player_1 = raw_input['Enter player 1 name: ']
player_2 = raw_input['Enter player 2 name: ']
current_space1 = 0
current_space2 = 0

# ...

def roll1[]:
    global current_space1  # instead of current_space1 = int[]
    # rest of the function ...

def roll2[]:
    global current_space2  # instead of current_space2 = int[]
    # rest of the function ...
3 và
finish_line = 20
player_1 = raw_input['Enter player 1 name: ']
player_2 = raw_input['Enter player 2 name: ']
current_space1 = 0
current_space2 = 0

# ...

def roll1[]:
    global current_space1  # instead of current_space1 = int[]
    # rest of the function ...

def roll2[]:
    global current_space2  # instead of current_space2 = int[]
    # rest of the function ...
4 đều là các biến cục bộ - chúng được tạo và khởi động khi hàm nhập và chỉ có sẵn trong bối cảnh chức năng, một khi bạn rời khỏi hàm hoặc chức năng nhập khác, chúng không khả dụng. Nếu bạn muốn chúng có sẵn bên ngoài chức năng [và tăng cường trong nhiều cuộc gọi chức năng], bạn cần phải khai báo chúng trên toàn cầu. biến cục bộ - chúng được tạo và khởi tạo khi hàm đi vào và chỉ khả dụng trong ngữ cảnh hàm, khi bạn rời khỏi hàm hoặc nhập một hàm khác, chúng sẽ không khả dụng. Nếu bạn muốn chúng khả dụng bên ngoài hàm [và tăng dần trong nhiều lệnh gọi hàm], bạn cần khai báo chúng trên toàn cầu

finish_line = 20
player_1 = raw_input['Enter player 1 name: ']
player_2 = raw_input['Enter player 2 name: ']
current_space1 = 0
current_space2 = 0

# ...

def roll1[]:
    global current_space1  # instead of current_space1 = int[]
    # rest of the function ...

def roll2[]:
    global current_space2  # instead of current_space2 = int[]
    # rest of the function ...

Phải nói rằng, sử dụng biến toàn cầu không được coi là một thành phần thực sự tốt. Ví dụ, xem bài viết này và trả lời câu trả lời của nó

Đã trả lời ngày 5 tháng 5 năm 2017 lúc 22. 28 5 tháng 5 năm 2017 lúc 22. 28

Mbydmbyd MByD

Huy hiệu vàng 134K2626 huy hiệu vàng264 huy hiệu bạc274 huy hiệu đồng

finish_line = 20
player_1 = raw_input['Enter player 1 name: ']
player_2 = raw_input['Enter player 2 name: ']
current_space1 = 0
current_space2 = 0

# ...

def roll1[]:
    global current_space1  # instead of current_space1 = int[]
    # rest of the function ...

def roll2[]:
    global current_space2  # instead of current_space2 = int[]
    # rest of the function ...
3 và
finish_line = 20
player_1 = raw_input['Enter player 1 name: ']
player_2 = raw_input['Enter player 2 name: ']
current_space1 = 0
current_space2 = 0

# ...

def roll1[]:
    global current_space1  # instead of current_space1 = int[]
    # rest of the function ...

def roll2[]:
    global current_space2  # instead of current_space2 = int[]
    # rest of the function ...
4 là các biến cục bộ của các hàm
finish_line = 20
player_1 = raw_input['Enter player 1 name: ']
player_2 = raw_input['Enter player 2 name: ']
current_space1 = 0
current_space2 = 0

# ...

def roll1[]:
    global current_space1  # instead of current_space1 = int[]
    # rest of the function ...

def roll2[]:
    global current_space2  # instead of current_space2 = int[]
    # rest of the function ...
7 và
finish_line = 20
player_1 = raw_input['Enter player 1 name: ']
player_2 = raw_input['Enter player 2 name: ']
current_space1 = 0
current_space2 = 0

# ...

def roll1[]:
    global current_space1  # instead of current_space1 = int[]
    # rest of the function ...

def roll2[]:
    global current_space2  # instead of current_space2 = int[]
    # rest of the function ...
8 tương ứng. Các biến thực sự được tăng lên theo dòng

finish_line = 20
player_1 = raw_input['Enter player 1 name: ']
player_2 = raw_input['Enter player 2 name: ']
current_space1 = 0
current_space2 = 0

# ...

def roll1[]:
    global current_space1  # instead of current_space1 = int[]
    # rest of the function ...

def roll2[]:
    global current_space2  # instead of current_space2 = int[]
    # rest of the function ...
0 nhưng trong dòng tiếp theo, bạn đang gọi hàm một lần nữa có phạm vi riêng của nó trong đó
finish_line = 20
player_1 = raw_input['Enter player 1 name: ']
player_2 = raw_input['Enter player 2 name: ']
current_space1 = 0
current_space2 = 0

# ...

def roll1[]:
    global current_space1  # instead of current_space1 = int[]
    # rest of the function ...

def roll2[]:
    global current_space2  # instead of current_space2 = int[]
    # rest of the function ...
1 là 0 lúc bắt đầu. Điều này sẽ tiếp tục cho đến khi bạn đạt đến mức tối đa. Độ sâu đệ quy

Thay vào đó, bạn có thể tạo các biến đó

finish_line = 20
player_1 = raw_input['Enter player 1 name: ']
player_2 = raw_input['Enter player 2 name: ']
current_space1 = 0
current_space2 = 0

# ...

def roll1[]:
    global current_space1  # instead of current_space1 = int[]
    # rest of the function ...

def roll2[]:
    global current_space2  # instead of current_space2 = int[]
    # rest of the function ...
2 và đánh dấu chúng như vậy với từ khóa
finish_line = 20
player_1 = raw_input['Enter player 1 name: ']
player_2 = raw_input['Enter player 2 name: ']
current_space1 = 0
current_space2 = 0

# ...

def roll1[]:
    global current_space1  # instead of current_space1 = int[]
    # rest of the function ...

def roll2[]:
    global current_space2  # instead of current_space2 = int[]
    # rest of the function ...
2

Một cách tiếp cận khác là tạo ra một lớp

finish_line = 20
player_1 = raw_input['Enter player 1 name: ']
player_2 = raw_input['Enter player 2 name: ']
current_space1 = 0
current_space2 = 0

# ...

def roll1[]:
    global current_space1  # instead of current_space1 = int[]
    # rest of the function ...

def roll2[]:
    global current_space2  # instead of current_space2 = int[]
    # rest of the function ...
4
finish_line = 20
player_1 = raw_input['Enter player 1 name: ']
player_2 = raw_input['Enter player 2 name: ']
current_space1 = 0
current_space2 = 0

# ...

def roll1[]:
    global current_space1  # instead of current_space1 = int[]
    # rest of the function ...

def roll2[]:
    global current_space2  # instead of current_space2 = int[]
    # rest of the function ...
3

and only use a function

finish_line = 20
player_1 = raw_input['Enter player 1 name: ']
player_2 = raw_input['Enter player 2 name: ']
current_space1 = 0
current_space2 = 0

# ...

def roll1[]:
    global current_space1  # instead of current_space1 = int[]
    # rest of the function ...

def roll2[]:
    global current_space2  # instead of current_space2 = int[]
    # rest of the function ...
5 cùng với hai trường hợp
finish_line = 20
player_1 = raw_input['Enter player 1 name: ']
player_2 = raw_input['Enter player 2 name: ']
current_space1 = 0
current_space2 = 0

# ...

def roll1[]:
    global current_space1  # instead of current_space1 = int[]
    # rest of the function ...

def roll2[]:
    global current_space2  # instead of current_space2 = int[]
    # rest of the function ...
4 được truyền dưới dạng đối số cho hàm
finish_line = 20
player_1 = raw_input['Enter player 1 name: ']
player_2 = raw_input['Enter player 2 name: ']
current_space1 = 0
current_space2 = 0

# ...

def roll1[]:
    global current_space1  # instead of current_space1 = int[]
    # rest of the function ...

def roll2[]:
    global current_space2  # instead of current_space2 = int[]
    # rest of the function ...
5 đó. Sau đó, bạn sẽ tăng____18

Ví dụ

finish_line = 20
player_1 = raw_input['Enter player 1 name: ']
player_2 = raw_input['Enter player 2 name: ']
current_space1 = 0
current_space2 = 0

# ...

def roll1[]:
    global current_space1  # instead of current_space1 = int[]
    # rest of the function ...

def roll2[]:
    global current_space2  # instead of current_space2 = int[]
    # rest of the function ...
2

BTW, bạn cũng có thể muốn suy nghĩ lại khi bạn kiểm tra người chơi đã đạt được mục tiêu vì trong ví dụ của bạn trước đây bạn yêu cầu lăn và sau đó người chơi có thể giành chiến thắng từ một số điểm đã đạt được

Đã trả lời ngày 5 tháng 5 năm 2017 lúc 22. 28 5 tháng 5 năm 2017 lúc 22. 28

a_guesta_guest a_guest

Mbydmbyd10 huy hiệu vàng57 huy hiệu bạc108 huy hiệu đồng

0

You can change a variable in a command if python?

Bạn không thể; . yêu tinh. Python chọn điều kiện đầu tiên phù hợp và sẽ coi thường các phần còn lại, bất kể bạn thực hiện thay đổi nào trong khối mã được chạy. cấu trúc elif . Python chọn điều kiện đầu tiên phù hợp và sẽ bỏ qua phần còn lại, bất kể bạn thực hiện thay đổi nào trong khối mã được chạy.

You can set a variable in a command IF?

Nếu bạn chưa quen với cú pháp được sử dụng trong mã mẫu, nếu [int i = 5] {là một cách hoàn toàn hợp lệ để khai báo và xác định một biến, thì hãy sử dụng nó bên trong . Nó cho phép chúng tôi viết mã terser, rõ ràng hơn, đồng thời tránh giới hạn phạm vi của một biến. if [int i = 5] { là một cách hoàn toàn hợp lệ để khai báo và xác định một biến, sau đó sử dụng nó bên trong câu lệnh if đã cho . Nó cho phép chúng ta viết mã ngắn hơn, rõ ràng hơn, đồng thời tránh giới hạn phạm vi của một biến.

Bạn có thể tạo một biến bên trong một câu lệnh nếu python không?

Python cho phép tạo các biến trong một if. Elif. khác. đề, để chúng có thể được sử dụng bên ngoài định mệnh khối có điều kiện. Điều này cũng đúng cho các điều kiện hòa nhau. elif. khác. mệnh đề , để chúng cũng có thể được sử dụng bên ngoài khối điều kiện. Điều này cũng đúng với các điều kiện lồng nhau.

Bạn có thể sửa đổi một biến trong Python không?

Một số giá trị trong Python có thể được sửa đổi, và một số không thể. Điều này không bao giờ có nghĩa là chúng ta không thể thay đổi giá trị của một biến - nhưng nếu một biến chứa giá trị của một loại bất biến, chúng ta chỉ có thể gán cho nó một giá trị mới. Chúng ta không thể thay đổi giá trị hiện tại theo bất kỳ cách nào

Chủ Đề