社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Rarblack  »  全部回复
回复总数  3
7 年前
回复了 Rarblack 创建的主题 » 不确定如何计算要显示的特定值(python/django)

首先,你应该明白 get() 和 filter() 是的。看一看 this 是的。

之后我们可以做一些改变:

def checkout(request):
    try:
        current_order = Order.objects.filter(owner__exact=1, status__icontains ="pre-place") # exact returns exact match, icontains(could have been iexact too if you want exact match) return not case sensitive match.
    except Order.DoesNotExist:
        return HttpResponse("Your current order is empty<br><a href=\"browse\">Go back</a>")
    else:
        items = OrderDetail.objects.filter(orderID__exact=current_order) #since it is id no need for iexact which is case insensitive.
        order_details = {} # it is always suggestible to use dictionary instead of turple for easiness.
        for item in items:
            weight = item.supplyID.weight * item.quantity
            order_details[item] = weight

        total_weight = sum(order_detail.values()) #sum of total weight

        context = { #clear to read and maintain
            'order_details': order_details,
            'current_order': current_order,
            'total_weight': total_weight
                                          }

        return render(request, 
                              'store/checkout.html', # i don't find storing url usefull
                                            context=context)

这是您的模板:

<h1>Your current order</h1>
    <a href="{% url 'Store:browse' %}">return to selecting 
 supplies</a><br><br>
    <table>
        <tr><th>name</th><th>item weight(kg)</th><th>qty</th><th>total 
 weight(kg)</th></tr>
        {% for item, weight in order_details.items() %}
            <tr>
                <td>{{ item.supplyID.name }}</td>
                <td>{{ item.supplyID.weight }}</td>
                <td>{{ item.quantity }}</td>
                <td>{{ weight }}</td>
            </tr>

        {% endfor %}
    </table>
7 年前
回复了 Rarblack 创建的主题 » 我的python camel游戏的变量不是udating

您的变量应该不在while循环中,这样就不会一遍又一遍地得到相同的值,而且您还应该每次都更新食堂饮料变量。

更新代码

def main ():
    global executed
    executed = 1
    if executed == 1: # This will only show the intro once
        print("Welcome to Camel!")
        print("You have stolen a camel to make your way across the great Mobi desert.")
        print("The natives want their camel back and are chasing you down!")
        print("Survive your desert trek and out run the natives.")
    global done  # Add global next to a variable if you're calling it from outside the function
    executed = 0

main()
import random

done = False

miles_traveled = int(0)
thirst = int(0)
camel_tiredness = int(0)
native_distance = int(-20)
canteen_drinks = int(3)
while not done:

    # Game variables


    print("A. Drink from your canteen.")
    print("B. Ahead moderate speed.")
    print("C. Ahead full speed.")
    print("D. Stop for the night.")
    print("E. Status check.")
    print("Q. Quit.")

    user_choice = (input("What is your choice? "))
    # Quit the game
    if user_choice.upper() == "Q":
        done = True
        print("Quitting...")
    # Status check
    elif user_choice.upper() == "E":
        print("Miles traveled:", miles_traveled)
        print("Drinks in Canteen:", canteen_drinks)
        print("The natives are", native_distance, "miles behind you.")
    # Stopping for the night
    elif user_choice.upper() == "D":
        camel_tiredness = int(0)
        print("Your camel is happy.")
        native_distance += random.randint(7, 14)
        print("The natives are", native_distance, "miles behind you.")
    #Full speed
    elif user_choice.upper() == "C":
        thirst += int(1)
        camel_tiredness += int(1)

        miles_traveled += random.randint(10, 20)
        print("You have traveled", miles_traveled, "miles.")
        native_distance += random.randint(7, 14)
    # Moderate speed
    elif user_choice.upper() == "B":
        miles_traveled += random.randint(5, 12)
        print("You have traveled", miles_traveled,"miles.")
        thirst += int(1)
        camel_tiredness += int(1)
        native_distance += random.randint(7, 14)
     # Drinking from canteen
    elif user_choice.upper() == "A":
        if canteen_drinks >= 1:
            print("You have taken a sip from your canteen")
            canteen_drinks = canteen_drinks - int(1) # you should update the centeen_drinks
            print("You have", canteen_drinks, "sips left in your canteen.")
        elif canteen_drinks == 0:
            print("You are out of water.")
    # The other stuff at the end
    if thirst > 4 < 6:
        print("You are thirsty.")

    if thirst > 6:
        print("You have died of thirst!")
        done = True

    if camel_tiredness > 5 < 8:
        print("Your camel is tired.")

    if camel_tiredness > 8:
        print("Your camel has died.")

    if native_distance >= 0:
        print("The natives have caught you.")
        done = True

    if native_distance >= -15:
        print("The natives are getting close!")

    if miles_traveled >= 200:
        print("You have won and got away from the natives!")
        done = True

    oasis = random.randint(0, 20)

    if oasis == 20:
        print("You have found an oasis.")
        thirst = 0
        camel_tiredness = 0
7 年前
回复了 Rarblack 创建的主题 » Pycharm在编辑器中打印字母而不是数字

您应该配置pycharm的控制台字体,使其按您的意愿显示。去 File->Settings->Editor->Font 并添加所需的字体。 enter image description here